13
votes
Accepted
Why del cursor/row objects of ArcPy?
Those are relics of an earlier style of arcpy cursors. del row, cursor were previously used to clean-up after the script was run by deleting the row and cursor objects. Now, the proper usage is to ...
13
votes
Accepted
Deleting cursor used in SearchCursor within dictionary comprehension?
Whether it's absolutely necessary is the wrong question to ask. The question is whether it's a good idea.
As a rule in programming, you should avoid doing weird things and use the best tool for the ...
12
votes
Accepted
Filling dictionary with list of row attributes using arcpy.SearchCursor
Consider using an arcpy.da.SearchCursor:
FieldNameDict = {}
with arcpy.da.SearchCursor(dbf,[myfield,'AP_ZIP','AP_STATE','AP_CITY','AP_FULLADD','AP_BUILDIN']) as rows:
for row in rows:
...
11
votes
Accepted
Does arcpy.da.SearchCursor store the results in an array?
As described in SearchCursor-Help, SearchCursor returns iterator. It means that you cannot use cursor[0][1]. In any case, you have to use for loop to iterate over features.
But using list ...
11
votes
Filling dictionary with list of row attributes using arcpy.SearchCursor
I have not tested this but it is a one liner that uses dictionary comprehension, an arcpy.da.SearchCursor() and the indexing syntax from the answer by @MichaelStimson:
FieldNameDict = {row[0]:[row[1:]]...
9
votes
ArcPy UpdateCursor auto pausing after every 1000 records
By default the auto commit interval is set to 1000. You can change the interval using the following:
arcpy.env.autoCommit = 5000
9
votes
UpdateCursor RuntimeError: An invalid SQL statement was used
There are several issues with your code:
As @Vince points out your list of fields that you supply to the cursor is incorrect
You are deleting the cursor within the loop
You are using the with ...
8
votes
Accepted
Looping if condition in ArcPy?
Try using an UpdateCursor:
import arcpy
stands = r'D:\mountaine\database.mdb\testPoly'
arcpy.MakeFeatureLayer_management(stands,'pormap')
with arcpy.da.UpdateCursor("pormap", [ 'TEXTSTR', 'NUMBR', '...
8
votes
Does arcpy.da.SearchCursor store the results in an array?
If you want to work with feature attributes as an array and do away with the computationally expensive for loop, you can use TableToNumPyArray() to convert the attributes to a numpy array. This ...
8
votes
Calling specific row using ArcPy search/update cursors
As commented by @hornbydd try using a where clause on your cursor.
For example:
import arcpy
fc = 'c:\example.shp'
fields = ['Value']
where_clause = "FID = 10"
with arcpy.da.SearchCursor(fc,...
8
votes
Accepted
Refer to field name instead of index position in ArcPy cursor
You could use tuple unpacking
flds = ["ASSET_ID","ROAD_ID","CLASS","OWNER","SHAPE@"]
with arcpy.da.SearchCursor(feature_class, flds) as rows:
for ...
7
votes
Accepted
'TypeError: 'tuple' object does not support item assignment' when iterating through the rows of a layer
You appear to be trying to update values in your data using a Search Cursor arcpy.da.SearchCursor() rather than an Update Cursor arcpy.da.UpdateCursor()
Try changing to an Update Cursor:
#...
7
votes
Accepted
Insert Cursor Having an Issue With Reading the Syntax of a List
The input to insertRow() must be a list or tuple.
Instead of building this as a string value as listed above,
dashboard_site_summary_list.append('((' + str(row[0]) + ',),')
append a tuple:
...
6
votes
Iterate through features to use selection as input for ExtractByMask
The SearchCursor will return geometries with the SHAPE@ token which can be used as extracting features etc.:
SHAPE@ —A geometry object for the feature.
import arcpy
feature_class = r'C:\test.gdb\...
6
votes
Improving poor performance of ArcPy update cursor with ArcSDE and Editor?
The nested cursors are slowing you way down. Instead, use a searh cursor to create a dictionary with UIDs as your key and rows as your values. Then use an update cursor to update your second feature ...
6
votes
arcpy.da.SearchCursor not work when run the whole scripts in IDLE
I suspect your error is that:
env.workplace = "E:/"
should be:
env.workspace = "E:/"
6
votes
Accepted
Updating Field using similar Field w/ Update Cursor: Field does not populate
You need to interact with the row object, not fields. Consider the example from UpdateCursor:
import arcpy
fc = 'c:/data/base.gdb/well'
fields = ['WELL_YIELD', 'WELL_CLASS']
# Create update cursor ...
6
votes
Extracting values of some columns with ArcPy
Replace list.append(x) with list.append(x[0])
x is a tuple of 1 and when you add those to lists or convert to strings you get that (x,) notation.
6
votes
UpdateCursor RuntimeError: An invalid SQL statement was used
You dont need to iterate over rows that are None/NULL or doesnt contain - so you can limit the rows returned by the cursor using a where_clause.
And when using with there is no need to delete the ...
6
votes
Accepted
Using wildcard to find similar values between two fields with ArcPy cursor
Firstly you CAN do a wild card search of your data with a select by attribute tool, I created a short blog over on ESRI Geonet which you should read.
In your case the expression would be:
FullStreet ...
6
votes
Calling specific row using ArcPy search/update cursors
The question has been answered, this is just another way which is handy if you need to retrive many values. Load all your data into a dictionary:
import arcpy
fields = ['FID','BK']
fc = r'C:\GIS\data\...
6
votes
Accepted
Shapefile not Z-enabled although the environment is Z-enabled in ArcPy
Raster to Point doesn't add Z information, it just creates an attribute in the output 2D feature class's attribute table. From the Raster To Point documentation:
Converts a raster dataset to point ...
5
votes
Accepted
Float not iterable
Putting the comments together:
import arcpy
arcpy.env.workspace = "c:/esripress/python/data"
fc = "Hawaii.shp"
with arcpy.da.SearchCursor(fc, ["OID@","SHAPE@"]) as cursor:
for row in cursor:
...
5
votes
Accepted
StopIteration: iteration not started error in arcpy.da.UpdateCursor
It looks like you're deleting a row if conditions are met, and then trying to update the same row following, which no longer exists. You can't update a row after it's been deleted.
Try:
fieldList = [...
5
votes
Accepted
Save a selected row into a new shapefile with arcpy
The cursor does not select anything, it is only returning a tuple (~a list of the attributes). But you can use the ObjectID returned by the cursor and pass this to Select:
Extracts features from an ...
5
votes
Accepted
Updating Field Based on another Field using UpdateCursor?
Edit your csv and remove everyting but the Sapno values and then execute code below. It will check if the value in field Property_Ref exists in the csv file and update.
import arcpy, csv
fc = r'C:\...
5
votes
Looping if condition in ArcPy?
I think you have a misunderstanding of cursors and calculate field, these methods are mutually exclusive (if you use one don't use the other).
The answer by Richard Morgan is correct, however there ...
5
votes
Accepted
Adding field which, depending on direction field value, gets value from previous or next row/feature in another field?
A cursor can only look at one row at a time but you can use two: Store all AP values in a list using a SearchCursor. Then use indexes to fetch values from the list and update using UpdateCursor. ...
5
votes
Accepted
Can't assign a numeric value to a field that is set to <Null>
SearchCursor returns a read-only cursor (so all rows are also read-only). You would need to use UpdateCursor instead.
The object returned by SearchCursor is a tuple - similar to a list - , and each ...
5
votes
Accepted
Joining several fields into single field using ArcPy?
You are wasting time creating a list set and sorting it and then running a cursor over and over to match one value at a time from the ordered data. The dictionary eliminates all of that. The ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
cursor × 1058arcpy × 1013
arcmap × 75
arcgis-10.2 × 64
arcgis-10.1 × 60
python × 57
arcgis-pro × 56
arcgis-desktop × 55
update × 49
geometry × 39
arcgis-10.0 × 38
arcgis-10.3 × 36
dictionary × 33
loop × 29
attribute-table × 27
fields-attributes × 25
runtimeerror × 25
field-calculator × 24
feature-class × 23
performance × 22
python-2.7 × 22
sql × 20
select-by-location × 20
select-by-attribute × 19
enterprise-geodatabase × 18