Python/ArcPy – Editing Attribute Tables

arcgis-10.0arcpyattribute-tableediting

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?

Best Answer

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.

Related Question