1

I have a function I'm trying to incorporate in a model. It's supposed to check to see if one of two fields have a value and depending on the field with value calculate the other.

The issue I'm having is writing the result back into my feature using the UpdateCursor. I don't have any experience using it, and not sure if it's the right tool for the job. Here's my function:

import arcpy

upCursor = arcpy.da.UpdateCursor("SepticTankPermit layer", ['pid','pidgis'])

def autoCheck():
 with arcpy.da.SearchCursor("SepticTankPermit layer",['pid','pidgis']) as cursor:
 for row in cursor:
  if row[0] is 'NULL' or row[0] == '':
    pidgis = row[1]
    upCursor.updateRow([pidgis[:2]+'-'+pidgis[4:6]+'-'+pidgis[6:7]+'-'+pidgis[7:10]+'-'+pidgis[10:13]+'.'+pidgis[13:]],[pidgis])
  elif row[1] is 'NULL' or row[1] == '':
    pid = row[0]
    upCursor.updateRow([pid],[pid[:2]+'00'+pid[3:5]+ pid[6:7]+ pid[8:11]+ pid[12:15]+ pid[16:]])
  else:
    print "PID or PIDGIS must have a value in order to execute properly!"

I'm not sure if I'm using it correctly.

1 Answer 1

3

There are a couple things wrong with this code snippet. First, you don't need to specify a SearchCursor just to look through a table. The UpdateCursor has the ability to read row values and update through a single iteration. Secondly, when assigning a value to a row, you need to set the index of the field to the value (row[1] = row[0][:2] + ' pid') based on the index set in the cursor (line 2). Lastly, the UpdateCursor updates rows one at a time after each iteration... meaning you set the values of each field, then update the entire row (cursor.updateRow(row)) specified in line 3.

import arcpy
with arcpy.da.UpdateCursor("SepticTankPermit layer",['pid','pidgis']) as cursor:
    for row in cursor:
        if row[0] in [None,'','NULL']:
            row[0] = row[1][:2]  #etc...
        elif row[1] in [None,'','NULL']:
            row[1] = row[0][:2] #etc...
        else:
            print "Row not updated..."
        cursor.updateRow(row)
del cursor 
1
  • Thanks for the help and the explanation. It makes more sense the way you've written it. Best of all I've got it working. Appreciate your help. Commented Jan 3, 2018 at 18:58

Your Answer

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

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