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.
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 = (row*(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 = (row*(28*28))/(valuedict[keyvalue][0])*100
upcur.updateRow(row)
landuse_percentage(watershed, output_pivot)Python: Code (Not working)
Solved! Go to Solution.
Hi Luke
Not a problem, thanks for your help.