1

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
0

1 Answer 1

8

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)
2
  • 1
    you'll need uc.updateRow(thing) instead of cursor
    – mr.adam
    Commented Apr 8, 2015 at 21:57
  • Ha, yes. I'm so used to my own syntax ;)
    – Erica
    Commented Apr 8, 2015 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.