0

I'm pretty new to Python scripting (especially in ArcGIS). I would like to access a single element from a field and modify it. I understand that I can do this with cursors by searching the field column for the value (arcpy.da.SearchCursor) and then replacing it (arcpy.da.UpdateCursor), but this feels inefficient.

Is there a way to directly access an element without looping continuously?

1
  • 1
    You don't need a SearchCursor with an UpdateCursor. Use a where_clause when you call the UpdateCursor to limit it to only the records you're interested in.
    – user2856
    Commented Apr 15, 2021 at 22:58

1 Answer 1

2

As commented by @user2856 using a where_clause on your UpdateCursor is all that is needed.

If you have variables fc, fld and val set to a feature class, a field name in that feature class, and a value in a text field respectively then this should work to change the value to a new value of "X":

with arcpy.da.UpdateCursor(fc,fld,"{0} = '{1}'".format(fld,val)) as cursor:
    for row in cursor:
        row[0] = 'X'
        cursor.updateRow(row)

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.