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.

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 

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.
{"cc_re__cc_re__cc_by_sa_4_0": ["-item\"><a href=\"https://twitter.com/stackoverflow\" class=\"-link js-gps-track\" data-gps-track=\"footer.click({ location: 2, link: 32 })\">Twitter</a></li>\r\n                    <li class=\"-item\"><a href=\"https://linkedin.com/company/stack-overflow\" class=\"-link js-gps-track\" data-gps-track=\"footer.click({ location: 2, link: 33 })\">LinkedIn</a></li>\r\n                </ul>\r\n\r\n                <p class=\"mt-auto mb24\">\r\nsite design / logo &#169; 2019 Stack Exchange Inc; user contributions licensed under <a href=\"https://creativecommons.org/licenses/by-sa/4.0/\" class=\"js-gps-track\" data-gps-track=\"footer.click({ location: 2, link: 34 })\" rel=\"license\">cc by-sa 4.0</a>\r\n                            with <a href=\"https://stackoverflow.blog/2009/06/25/attribution-required/\"  class=\"js-gps-track\" data-gps-track=\"footer.click({ location: 2, link:35 })\" rel=\"license\">attribution required</a>.\r\n                    <span id=\"svnrev\">rev&nbsp;2019.9.16.34922</span>\r\n                </p>\r\n            </div>\r\n        </div>\r\n\r\n            </footer>\r\n            <script>StackExchange.ready(function () { StackExchange.responsiveness.addSwitcher(); })</scri"], "copyright__copyright__copyright": ["s']) as cursor:\n    for row in cursor:\n        if row[0] in [None,'','NULL']:\n            row[0] = row[1][:2]  #etc...\n        elif row[1] in [None,'','NULL']:\n            row[1] = row[0][:2] #etc...\n        else:\n            print \"Row not updated...\"\n        cursor.updateRow(row)\ndel cursor \n\nYour Answer\n\nBy clicking \u201cPost Your Answer\u201d, you agree to our terms of service, privacy policy and cookie policy\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}