I'm trying to use the updateCursor to insert a calculated value in the correct field. However, I keep on receiving an execution error. I think it is an easy fix, but I am new to arcpy and cannot figure out where I'm making a mistake.

Here's the code:

cursor = arcpy.UpdateCursor(outAgri)
for row in cursor:
    arcpy.AddMessage("Field " + str(count) + " of " + str(loop))
    agriFeat = arcpy.SelectLayerByAttribute_management(agri_lyr,       "NEW_SELECTION", '"OBJECTID_1" = ' + "%s" %count)
    count += 1

    marginbound = arcpy.SelectLayerByLocation_management (margin_lyr, "BOUNDARY_TOUCHES", agriFeat, "", "NEW_SELECTION")
    mCursor = arcpy.SearchCursor(marginbound, fields= "Margin_Type; Health_Index; Shape_Area")
    ##mFields = arcpy.ListFields(marginbound) 
    mcount = 0
    gArea = 0
    pArea = 0
    rArea = 0
    gHealth = 0
    pHealth = 0
    rHealth = 0

    for row in mCursor:

        if row.getValue("Margin_Type") == "G":
            gAreaTest = row.getValue("Shape_Area")
            gArea = (row.getValue("Shape_Area") + gArea)
            gHealth = row.getValue("Health_Index")

        if row.getValue("Margin_Type") == "P":
            arcpy.AddMessage("Planted Hedges")
            rArea = (row.getValue("Shape_Area") + rArea)
            rHealth = row.getValue("Health_Index")

        if row.getValue("Margin_Type") == "R":
            arcpy.AddMessage("Remnant Hedges")
            pArea = (row.getValue("Shape_Area") + pArea)
            pHealth = row.getValue("Health_Index")

        mcount += 1

    tArea = (pArea + gArea + rArea)

    fieldHealth = (((rArea/tArea) * rHealth) + ((gArea/tArea) * gHealth) + ((pArea/tArea) * pHealth))

    arcpy.AddMessage("The Field health is " + str(fieldHealth))
    row.setValue("Field_Health", fieldHealth)
  • You're only using searchcursors, and your indentation of the last few lines is outside your for loop -- is that a formatting issue? – Paul Apr 28 '16 at 19:40
  • row.setValue = (fieldHealth) should be row.setvalue(FIELDNAME, fieldHealth). You've set the value of fieldHealth right above this line, but I don't see the field/column name you want to set to. – dslamb Apr 28 '16 at 19:49
  • I added the field to the code so that it is now : row.setValue = ("Field_Health", fieldHealth) but I am now getting an "Invalid input value for setting" error message – Owen P Apr 28 '16 at 19:59
  • @Paul, I have two cursors in this code. The UpdateCursor at the very top and a SearchCursor embedded with in the first cursor – Owen P Apr 28 '16 at 20:01
  • @OwenP you don't need the equals sign. serValue is a function. Delete the = and make sure it is like row.setvalue(FIELDNAME, fieldHealth). – dslamb Apr 28 '16 at 20:10

I believe I figured out the issue. Because I had the SearchCursor embedded in the UpdateCursor, and used "row" as the variable in for loops for both cursors there was an issue when I used row.setValue(). Once I changed the UpdateCursor variables to "uRow" from "row", the fields successfully updated.

  • I'd laugh if I hadn't inadvertently done the same thing when I started using python... definitely a trap. Python doesn't give you error messages when you try to instantiate the same variable again like programming languages do. Like you said though the answer is to label your variables with uRow for update and sRow for search every time and get into the habit; there's seldom a need for two search, update or insert cursors at the same time. +1 for spotting this error yourself (trust me it's hard to find because there's nothing wrong with the syntax!) – Michael Stimson Apr 28 '16 at 22:22

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.