AnsweredAssumed Answered

error handling and cursors

Question asked by jwolff on Nov 27, 2018
Latest reply on Dec 4, 2018 by jwolff

I'm using the usaddress library to split single-line addresses into their various components. Below is the functioning script that I'm using for testing:

 

import arcpy
from arcpy import env
import os
import usaddress
from collections import OrderedDict

# Get input table through parameter
table = arcpy.GetParameterAsText(0)

# Get the field containing the single-line addresses through parameter
infield = arcpy.GetParameterAsText(1)

# List of fields to be updated
fields = ["addr", "addrNum", "stNm", "zip"]

with arcpy.da.UpdateCursor(table, fields) as cursor:
     for addr, addrNum, stNm, zip in cursor:
          try:
               parse = usaddress.tag(addr)[0]
               addrNum = parse.get("AddressNumber", "")
               stNm = parse.get("StreetName", "")
               zip = parse.get("ZipCode", "")
               cursor.updateRow([addr, addrNum, stNm, zip])
               continue
          except Exception:
               arcpy.AddMessage("Error")

 

Here's what the testing input table looks like before:

before

And after:

after

 

My goal is to write the records that error out to a separate table.  (Errors include situations where multiple areas of an address have the same label, like "5318 S 86 CT APT 3 APT 412, Omaha, NE 68137" - multiple APT entries, or "PO Box 291 PO BOX 291, Genoa, NE 68640" - multiple PO Box entries.)  I've reviewed the examples in the error handling page, but they seem to be very focused on geoprocessing.

 

Also, this is the first time I've used cursors in this manner, which prevents me from using something like the following:

 

     except Exception:
          arcpy.AddMessage(str(row.getValue("OBJECTID")) + " Error")     

 

At least I can't figure it out so far.  Any suggestions or bump in the right direction are greatly appreciated.

Thanks

Justin

Outcomes