I have a field called "CODE_12" in which the values are strings (3 characters). I have another empty field called "AE" and that I want to fill depending on the value on the "CODE_12" field. For example, when the CODE_12 field is equal to 112 I want the AE field to have a value of 396. I have tried using the UpdateCursor but I'm getting the error "Invalid input value for setting". Any idea of what I´m doing wrong? I attache my code

rows = arcpy.UpdateCursor(RainLandUnion) # This is my feature layer
for row in rows:
         if row.getValue("CODE_12") == "112":
            row.setValue("AE", 396.71)
         elif row.getValue("CODE_12") == "221":
         elif row.getValue("CODE_12") == "321":
             row.setValue=("AE", 438.2)
         elif row.getValue("CODE_12") == "242":
             row.setValue= ("AE",425.45)
         elif row.getValue("CODE_12") == "243":
             row.setValue= ("AE",425.45)
         elif row.getValue("CODE_12") == "324":
         elif row.getValue("CODE_12") == "313":
             row.setValue= ("AE",441.64)

The full error message is :

Message File Name Line Position Traceback
C:\Users\Administrator\Desktop\Nuenna\nuenna shapes\model_sample\ae_calc.py 51
setattr C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\arcobjects_base.py 35
exceptions.ValueError: Row: Invalid input value for setting

  • You should really look into using the Data Access (da) updatecursor - they're much faster. Details on that here: pro.arcgis.com/en/pro-app/arcpy/functions/updatecursor.htm I'm not 100 percent certain on this - but you may want to cast your numbers to string when you write them to the feature as in: row.setValue("AE",str(441.64)) – Kevin Aug 2 '16 at 23:10
  • 1
    @Kevin that link is for ArcGIS Pro. For ArcGIS 10.2 the link would be resources.arcgis.com/en/help/main/10.2/index.html#//… – Midavalo Aug 2 '16 at 23:12
  • Your indentation is slightly astray but I think the error may be saying that you are trying to push floating point values into a field that is defined as something else (e.g. long, short, text) – PolyGeo Aug 2 '16 at 23:19
  • @Kevin I have also tried with the DA updatecursor and it wasn't working, but anyway I would need to keep the AE field as double since I'll use it to calculate another field afterwards. – lia Aug 2 '16 at 23:19
  • @PolyGeo the field is defined as Double, and i fixed the indentation – lia Aug 2 '16 at 23:24

You have some = in the wrong place in some of your setValue lines - row.setValue("AE",435.920) in some lines, and row.setValue=("AE", 438.2) in others.

Also, try using a dictionary of update values rather than if/elif

Using arcpy.UpdateCursor():

updateValDict = {"112": 396.71, "221": 435.920, "321": 438.2, "242": 425.45, "243": 425.45, "324": 441.64, "313": 441.64}
rows = arcpy.UpdateCursor(RainLandUnion) # This is my feature layer
for row in rows:
    c12 = row.getValue("CODE_12")
    if row.getValue("CODE_12") in updateValDict:
        row.setValue("AE", updateValDict[c12])

Using arcpy.da.UpdateCursor():

updateValDict = {"112": 396.71, "221": 435.920, "321": 438.2, "242": 425.45, "243": 425.45, "324": 441.64, "313": 441.64}
fields = ["CODE_12", "AE"]
with arcpy.da.UpdateCursor(RainLandUnion, fields) as cursor:
    for row in cursor:
        if row[0] in updateValDict:
            row[1] = updateValDict[row[0]]
  • I'd not spotted those = signs. – PolyGeo Aug 3 '16 at 0:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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