Search code examples
python-2.7arcgisesri

Trouble using UpdateCursor when setting the value


Using ArcDesktop 10.1 & Python 2.7: I am working on a code that searches for values within 13 fields and, based on what it finds within those 13 fields, it concatenates a string and puts the result in an existing (empty) field.

It uses a search cursor to search the 13 fields. Then uses the result of that in an update cursor to concatenate the string.

I am having trouble getting the result into the field using the setValue - Line 40 of the code below @ urow.setValue(commentsField, easementType). The error message is very unhelpful (RuntimeError: ERROR 999999: Error executing function.)

I am not sure how to correctly get the value set in the field desired. Any help would be greatly appreciated!

import arcpy, os, math
from itertools import izip
arcpy.env.workspace = "C:\\Users\\mdelgado\\Desktop\\WorkinDog.gdb"

#These are my variables
fc = "EASEMENTS"
commentsField = "Comments"
typeFields = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMM", "DRAIN", "ELEC", "GEN_UTIL", "LANDSCAPE", "PARKING", "PIPELINE", "SAN_SEWR", "SIDEWALK", "SPECIAL", "STM_SEWR", "WATER"]
fieldNames = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMMUNICATION", "DRAINAGE", "ELECTRIC", "GENERAL UTILITY", "LANDSCAPE", "PARKING", "PIPELINE", "SANITATION SEWER", "SIDEWALK", "SPECIAL", "STORM SEWER", "WATER"]
fieldValues = []
easementType = ""

#This is my search cursor
scursor = arcpy.SearchCursor(fc)
srow = scursor.next()
for field in typeFields:
    srowValue = (srow.getValue(field))
    fieldValues.append(srowValue)
    srow = scursor.next()
print fieldValues

#This is my update cursor
ucursor = arcpy.UpdateCursor(fc)
for urow in ucursor:

    #This is where I begin the loop to concatenate the comment field
    for (value, name) in izip(fieldValues, fieldNames):
        print str(value) + " " + name

        #This is where I check each field to find out which types the easement is
        if value == 1:
            easementType = easementType + name + ", "

    #This is where I format the final concatenated string
    easementType = easementType[:-2]
    print easementType

    #This is where the field is updated with the final string using the cursor
    urow.setValue(commentsField, easementType)
    ucursor.updateRow(urow)
    urow = cursor.next()

del urow
del ucursor
del srow
del scursor

Solution

  • The uninformative 999999 error is one of the worst.

    I suggest a couple modifications to your approach that may make it simpler to troubleshoot. First, use the da Cursors -- they are faster, and the syntax is a little simpler.

    Second, you don't need a separate Search and Update -- the Update can "search" other fields in the same row in addition to updating fields. (The current code, assuming it was working correctly, would be putting the same fieldValues into every row the UpdateCursor affected.)

    fieldNames = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMMUNICATION", "DRAINAGE",
                  "ELECTRIC", "GENERAL UTILITY", "LANDSCAPE", "PARKING", "PIPELINE", "SANITATION SEWER",
                  "SIDEWALK", "SPECIAL", "STORM SEWER", "WATER"]
    cursorFields = ["ABANDONED", "ACCESS", "AERIAL", "BLANKET", "COMM", "DRAIN", 
                    "ELEC", "GEN_UTIL", "LANDSCAPE", "PARKING", "PIPELINE", "SAN_SEWR",
                    "SIDEWALK", "SPECIAL", "STM_SEWR", "WATER", "Comments"]
    
    with arcpy.da.UpdateCursor(fc, cursorFields) as cursor:
        for row in cursor:
            easementType = ""
            for x in range(13):
                if row[x] == 1:
                    easementType += fieldNames[x] + ", "
            easementType = easementType[:-2]
            print easementType
    
            row[13] = easementType
            cursor.updateRow(row)