[GIS] Using Python strip and split functions in ArcGIS Field Calculator

arcgis-desktopfield-calculatormodelbuilderpython-parser

I am trying to populate a field with the last word in an address string using field calculator within ModelBuilder. My code takes the whole address string (1234 S Brittany Town Dr) as a parameter, then splits it into a list and returns the last item in the list:

def getSufDir( prop_location ):
    prop_location.strip()
    x = prop_location.split(' ')
    return x[len(x)-1]

This code works for 433 of the 37,000 records in the layer. I discovered that those 433 records have 25 characters (prop_location is a 25 character maximum field). All other records return blank. Why is this happening? These code snippets seem to work fine in IDLE, but return blank values for most records when done in Field Calculator. I have attached an image of my field calculator window for reference.

enter image description here

An example of an address that is failing to process is 6847 W Mesa Arch Dr.

I edited to code to:

return x[-1] 

and got an identical result.

Best Answer

There were some records that were blank which caused an error. This code works:

def getSufDir( prop_location ):
if prop_location is None:
    return None
else:
    return prop_location.strip().split(" ")[-1]
Related Question