Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

arcpy.da.updatecursor with list

$
0
0
I'm using the arcpy.da module with the updatecursor class to populate specific values for specific fields in several hundred shapefiles. I'm specifiying the fields of interest for the updatecursor in a list but the catch is that the fields in the list are not all present in every shapefile. So when the cursor trys to iterate through the fields of each shapefile and a field doesn't exist it throws an error saying the 'column is not specified'. I tried several ways of verifying if the fields exists but have had not luck. Any suggestions, see code below. Thanks

Code:

def field_vals():
    env.workspace = 'some_wspace'
    field_list = ['field1', 'field2', 'field3', 'field4', 'field5']
    val_list =[23,45,34,99,76]
    fcList = arcpy.ListFeatureClasses()
    for fc in fcList:
        with arcpy.da.UpdateCursor(fc,field_list) as cursor:
            for row in cursor:
                if row[4] == 0 and row[0] not in val_list and row[0] != 0:
                    row[4] = row[1]
                elif row[4] == 0 and row[1] not in val_list and row[1] != 0:
                    row[4] = row[2]
                elif row[4] == 0 and row[2] not in val_list and row[2] != 0:
                    row[4] = row[3]
                cursor.updateRow(row)
field_vals()


Viewing all articles
Browse latest Browse all 2485

Latest Images

Trending Articles



Latest Images