AnsweredAssumed Answered

Newly Added Field: Not being recognized in Python Function

Question asked by Playa on Jul 13, 2016
Latest reply on Jul 14, 2016 by Playa

I've added a new field "CatchAreaKm2" (Double) to a File Geodatabase Table. I then create a list of fields of the table that will be used in an UpdateCursor. The problem that I have is that the newly added field is listed within the list of fields yet when I iterate through a sliced version of the fields (landuse) it doesn't recognize the newly added "CatchAreaKm2" field and I can't understand for the life of me why not. In order the get a list of the Landuse fields the index slice should be row[1:-1] yet if I use the following it skips the last landuse field ignoring the newly added field. in order to get the following to work I need to using the index slice row[1:]

 

Any help solving why the newly field is not being recognized will be appreciated.

 

  • [u'HydroID', u'Commercial_Forestry', u'Cultivated', u'Indigenous', u'Mines', u'Natural_Vegetation_Forest', u'Urban', u'Waterbodies', u'Wetlands', u'CatchAreaKm2']

Python: landuse_fields

 

# calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
    watershed_fields = ["HydroID", "Shape_Area"]
    valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)}  # @UndefinedVariable
    arcpy.AddField_management(output_pivot, "CatchAreaKm2", "DOUBLE")
    landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
    print(landuse_fields)
    with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur:  # @UndefinedVariable
        for row in upcur:
            keyvalue = row[0]
            if keyvalue in valuedict:
                arcpy.AddMessage("Processing Watershed {0}".format(keyvalue))
                # add catchment area km² to newly added field
                row[-1] = valuedict[keyvalue][0]/1000000
                for i, landuse in enumerate(row[1:]): # index slice not picking up newly added field
                    row[i] = (row[i]*(28*28))/(valuedict[keyvalue][0])*100
                    upcur.updateRow(row)

landuse_percentage(watershed, output_pivot)

 

Python: Code (Currently working)

 

# calculate percentage of landuse per watershed
def landuse_percentage(watershed, output_pivot):
    watershed_fields = ["HydroID", "Shape_Area"]
    valuedict = {r[0]: (r[1:]) for r in arcpy.da.SearchCursor(watershed, watershed_fields)}  # @UndefinedVariable
    arcpy.AddField_management(output_pivot, "CatchAreaKm2", "DOUBLE")
    landuse_fields = [f.name for f in arcpy.ListFields(output_pivot)[1:]]
    print(landuse_fields)
    with arcpy.da.UpdateCursor(output_pivot, landuse_fields) as upcur:  # @UndefinedVariable
        for row in upcur:
            keyvalue = row[0]
            if keyvalue in valuedict:
                arcpy.AddMessage("Processing Watershed {0}".format(keyvalue))
                # add catchment area km² to newly added field
                row[-1] = valuedict[keyvalue][0]/1000000
                for i, landuse in enumerate(row[1:-1]): # index slice should be
                    row[i] = (row[i]*(28*28))/(valuedict[keyvalue][0])*100
                    upcur.updateRow(row)

landuse_percentage(watershed, output_pivot)

 

Python: Code (Not working)

Outcomes