1

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":
 arcpy.AddMessage("Grass")
 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)
 cursor.updateRow(row)
asked Apr 28, 2016 at 19:20
7
  • You're only using searchcursors, and your indentation of the last few lines is outside your for loop -- is that a formatting issue? Commented Apr 28, 2016 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. Commented Apr 28, 2016 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 Commented Apr 28, 2016 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 Commented Apr 28, 2016 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). Commented Apr 28, 2016 at 20:10

1 Answer 1

1

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.

answered Apr 28, 2016 at 21:06
1
  • 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!) Commented Apr 28, 2016 at 22:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.