Skip to main content
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 ...
Aaron's user avatar
  • 52k
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 ...
jpmc26's user avatar
  • 1,749
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: ...
Michael Stimson's user avatar
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 ...
Kadir Şahbaz's user avatar
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:]]...
PolyGeo's user avatar
  • 65.5k
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
Lorne Dmitruk's user avatar
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 ...
Hornbydd's user avatar
  • 45k
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', '...
Richard Morgan's user avatar
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 ...
Aaron's user avatar
  • 52k
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,...
PolyGeo's user avatar
  • 65.5k
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 ...
user2856's user avatar
  • 74.3k
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: #...
Midavalo's user avatar
  • 30k
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: ...
SMiller's user avatar
  • 3,726
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\...
Bera's user avatar
  • 82k
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 ...
Emil Brundage's user avatar
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:/"
PolyGeo's user avatar
  • 65.5k
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 ...
phloem's user avatar
  • 4,698
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.
Hornbydd's user avatar
  • 45k
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 ...
Bera's user avatar
  • 82k
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 ...
Hornbydd's user avatar
  • 45k
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\...
Bera's user avatar
  • 82k
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 ...
user2856's user avatar
  • 74.3k
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: ...
Michael Stimson's user avatar
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 = [...
Emil Brundage's user avatar
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 ...
Bera's user avatar
  • 82k
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:\...
Bera's user avatar
  • 82k
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 ...
Michael Stimson's user avatar
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. ...
Bera's user avatar
  • 82k
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 ...
JGH's user avatar
  • 44.4k
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 ...
Richard Fairhurst's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible