1

I'm pretty new to scripting with Python.

My code is intending to iterate through a list of feature classes within a GDB, creating the fields EF_Type and EF_Group, then calculating the field. The values of 'EF_Group' will be dependent on the values of EF_Type (ex: the feature classes 'Local EOCs' and 'State EOCs' will both have the value of 'EOC' in the 'EF_Group' field.

I created an update cursor to iterate through the feature classes and update the values of 'EF_Group.' The script runs without an error message, but when I open the attribute tables within Pro, 'EF_Group' remains NULL.

Code:

    arcpy.AddField_management (fc, 'EF_Type', 'TEXT')

    EFType = arcpy.CalculateField_management(fc, 'EF_Type', '"' + name + '"') #Renames field EF_Type to the first 2 words pulled using the split format tool
    arcpy.AddField_management (fc, 'EF_Group', 'TEXT')

    fields= ['EF_Type', 'EF_Group']

    with arcpy.da.UpdateCursor(fc, fields) as cursor: #Update cursor will use 1st field (EF_Type) to build 2nd field (EF_Group)
        for row in cursor:
            if fields[0] == 'Local EOCs' or fields[0] == 'State EOCs':
                fields[1] = 'EOC'
                cursor.updateRow(row)
            elif fields[0] == 'Fire Stations':
                fields[1] = 'Fire'
                cursor.updateRow(row)
            elif fields[0] == 'Public Schools' or fields[0] == 'Private Schools' or fields[0] == 'DayCare Centers' or fields[0] == 'Colleges Universities':
                fields[1] = 'School'
                cursor.updateRow(row)
            elif fields[0] == 'UrgentCare Centers' or fields[0] == 'Nursing Homes' or fields[0] == 'Hospitals Intersect':
                fields[1] = 'Care'
                cursor.updateRow(row)
            elif fields[0] == 'LawEnforcement Locations':
                fields[1] = 'Law'
                cursor.updateRow(row)
2
  • 4
    You never actually change the row object. You're only changing fields, but you need to change row.
    – phloem
    Commented Jun 1, 2018 at 18:31
  • 1
    At first glance it looks you may want to change fields[x] to row[x] where x is 0 or 1
    – ZrSiO4
    Commented Jun 1, 2018 at 18:34

1 Answer 1

6

You need to interact with the row object, not fields. Consider the example from UpdateCursor:

import arcpy

fc = 'c:/data/base.gdb/well'
fields = ['WELL_YIELD', 'WELL_CLASS']

# Create update cursor for feature class 
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    # For each row, evaluate the WELL_YIELD value (index position 
    # of 0), and update WELL_CLASS (index position of 1)
    for row in cursor:
        if (row[0] >= 0 and row[0] <= 10):
            row[1] = 1
        elif (row[0] > 10 and row[0] <= 20):
            row[1] = 2
        elif (row[0] > 20 and row[0] <= 30):
            row[1] = 3
        elif (row[0] > 30):
            row[1] = 4

        # Update the cursor with the updated list
        cursor.updateRow(row)
0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.