This is a very simple task to do but I can't understand the right syntax.
I have a shapefile whose attributes are similar to the following:
FID Shape FIELD1 FIELD2 FIELD3 ... 0 Polygon 0 1 0 1 Polygon 3 0 7 2 Polygon 3 4 7 ...
The number of fields and their names are always different.
I need to create a new field (let's name it NUM), and count the number of zeros in each row.
FID Shape FIELD1 FIELD2 FIELD3 NUM 0 Polygon 0 1 0 2 1 Polygon 3 0 7 1 2 Polygon 3 4 7 0
I know how to create a new field, however I am not clear on the next steps.
The working code:
#path is path to shape file def a(path): fields = arcpy.ListFields(path,"FID_*") #FID_* is wildcard to select a fields name arcpy.AddField_management(path, "NUM", "SHORT") #create a field with name NUM cursor= arcpy.UpdateCursor(path) for row in cursor: count=0 for field in fields: a= row.getValue(field.name) #take a value if a==0: #if value=0 then value=value+1 count+=1 row.setValue("NUM", count) cursor.updateRow(row) del row del cursor
Thanks blah238, now I can eat pythons!