I want to update a field in my feature class. I tried to use a da.SearchCursor (code below) but was quickly reminded that tuples are immutable... I ran the code but it failed with a TypeError: 'tuple' object does not support item assignment
.
How do I most efficiently retrieve all rows from a feature class and update a field of each row and save it back to the feature class?
with arcpy.da.SearchCursor(fc, fldNames) as sc:
for thing in sc:
tmp = 0
#
# do stuff here
#
thing[idxField] = tmp
1 Answer 1
You want to use an Update Cursor, not a Search Cursor. It allows the same retrieve rows and loop through process, but also enables updating fields.
with arcpy.da.UpdateCursor(fc, fldNames) as uc:
for thing in uc:
# assign the data to the various fields
thing[0] = tmp0
thing[1] = tmp1
# trigger an update (save) for that row
uc.updateRow(thing)