1. 程式人生 > >How to edit Vector attribute tables using Python/ArcPy?

How to edit Vector attribute tables using Python/ArcPy?

Method 1: arcpy.UpdateCursor

This should do it and is a little simpler than the examples in the online help for UpdateCursorwhich 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.

Method 2: arcpy.da.UpdateCursor