AnsweredAssumed Answered

How to call a variable instead of a field name with Update Cursor

Question asked by rgthurau on Mar 6, 2012
Latest reply on Mar 6, 2012 by rgthurau
Hi,
I want to write a code that will add a number of fields to a feature class and calculate those fields using update cursor based on other values within the table. Yes, sounds easy. But, I want to make a single list of the fields to add, then I want update cursor to use the list objects rather than having to write the names in the code. Please see below:

LcTab = r"X:Data\LcTab"  fieldList = ["For_Area", "For_Pct", "Dev_Area", "Dev_Pct", "FldPas_Area", "FldPas_Pct", "Wet_Area", "Wet_Pct", "Water_Area", "Water_Pct", "Barren_Area", "Barren_Pct", "Open_Area", "Open_Pct", "Deforest_Area", "Deforest_Pct", "Ice_Area", "Ice_Pct"] for field in fieldList:     arcpy.AddField_management(LcTab, field, "DOUBLE")  rows = arcpy.UpdateCursor(LcTab) for row in rows:     row.fieldList[0] == row.FOREST/1000     rows.updateRow(row)     row.fieldList[2] == row.DEVELOPED/1000     rows.updateRow(row)


Above instead of:

rows = arcpy.UpdateCursor(LcTab) for row in rows:     row.For_Area == row.FOREST/1000     rows.updateRow(row)     row.for_pct == row.DEVELOPED/1000     rows.updateRow(row)


The idea would be to have the update cursor that would run on any field list regardless of the actual names. The problem is update cursor does not require quotes, so how can I tell it to look at my list and subscript (fieldList[0]) instead of looking for a field name "fieldlist[0]"?

Here's my error code.

    row.fieldList[0] == row.FOREST/1000
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 25, in __getattr__
    x = getattr(self._arc_object, attr)
RuntimeError: Row: Field fieldList does not exist

Thanks for your input.

Rich

Outcomes