4

I am new to Python, so sorry for such a trivial question.

There is a shapefile (let's call it Structures.shp) and the script I'm writing needs to update the value of only one record in only one field (let's call the field structuretype).

The field is the subtype field and the default value is (for example) 3, but the script needs to change the value for this particular record to 4. What is the easiest way for the script to update the value of this one cell?

Bera
81.7k14 gold badges85 silver badges199 bronze badges
asked May 11, 2013 at 19:22
0

2 Answers 2

13

This should do it and is a little simpler than the examples in the online help for UpdateCursor which is nevertheless worth a read.

I've assumed your shapefile is in a folder called C:\temp and that structuretype is an integer field. If it is a text field just use "3" and "4" instead of 3 and 4.

import arcpy
features = arcpy.UpdateCursor(r"C:\temp\Structures.shp")
for feature in features:
 if feature.structuretype == 3:
 feature.structuretype = 4
 features.updateRow(feature)
del feature,features

Warning: The way I am referencing field values here only works for old style cursors and not for the superior and faster cursors in the Data Access (arcpy.da) module, which was introduced with ArcGIS 10.1.

nmpeterson
8,42334 silver badges60 bronze badges
answered May 11, 2013 at 20:27
0
0

Late to the party but I came across this post a few days ago looking for the same thing and figured it out (I'm on 10.5):

fc = r"C:/your/path"
field = ['structuretype']
with arcpy.da.UpdateCursor(fc, field) as cursor:
 for row in cursor:
 if row[0] == 3:
 row[0] = 4
 cursor.updateRow(row)
answered Nov 16, 2019 at 2:56

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.