Skip over or exclude empty variable in field calculator expression

648
10
Jump to solution
05-07-2018 11:36 AM
ARCADMIN
New Contributor II

Hello, I am trying to parse street addresses using the field calculator and return just the street name.  My expression works except for addresses where there is only a house number with no street name after it; the calculation throws an error when it gets to these bad records where the street name doesn't exist.  I would like to be able to skip over these bad addresses and just return the input string for them.  I have tried adding a second condition to my if statement, but cannot seem to get the syntax correct.  Essentially what I want to say is "if the house number is a digit and the street name is not blank, return the street name otherwise return the full input string"

def addressParser(inString):
    splitString = inString.split(' ',1)
    houseNumber = splitString[0]
    streetName = splitString[1]
    if houseNumber.isdigit() and if streetName != "":
        return streetName
    else:
        return inString

addressParser( !FULLADDRESS! )

Thanks in advance for any help

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

You could change your function to and check if length of the list is 1:

def addressParser(inString):
    splitString = inString.split(' ',1)
    if len(splitString) == 1:
        return inString
    else:
        houseNumber = splitString[0]
        streetName = splitString[1]
        if houseNumber.isdigit() and streetName != "":
            return streetName
        else:
            return inString

However, you might run into other "exceptions". Does your field only contain number and street?

View solution in original post

10 Replies
XanderBakker
Esri Esteemed Contributor

You could change your function to and check if length of the list is 1:

def addressParser(inString):
    splitString = inString.split(' ',1)
    if len(splitString) == 1:
        return inString
    else:
        houseNumber = splitString[0]
        streetName = splitString[1]
        if houseNumber.isdigit() and streetName != "":
            return streetName
        else:
            return inString

However, you might run into other "exceptions". Does your field only contain number and street?

ARCADMIN
New Contributor II

Thank you very much, that works!  Yes my addresses only contain house number and street name + type

0 Kudos
JoeBorgione
MVP Esteemed Contributor
def addressParser(inString):
    splitString = inString.split(' ',1)
    houseNumber = splitString[0]
    streetName = splitString[1]
    if houseNumber.isdigit() and if streetName != "":
        return streetName
    else:
        return inString

 If I'm reading this right, in line 2 you are giving it a 'maxsplit' value of 1, but I'm not sure why: I guess just to separate house number and the rest of the street?  Take a look at an older post of mine: A better way to parse an address?  it has worked for me for tons of address parsing.

In your case, you might check the len() of splitString so if it's longer than 1, you have a street with your house number:

instring = 1234 S MMAIN ST'
splitstring = instring.split(' ',1)
print splitstring
>>>['1234', 'S MAIN ST']
len(splitstring)
>>> 2

if len(splitstring) > 1:
   do what you need to do with a good address
elif len(splitstring)== 1:
    do what you need to do with just a house number
That should just about do it....
ARCADMIN
New Contributor II

Thank you very much that works!  Yes I only need to parse the house number and the rest of the street name not each individual component of the street name. 

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I highly encourage you to read through the thread referenced by Joe Borgione‌ (https://community.esri.com/thread/163731 ) .  If you are going to be working with addresses a lot, I recommend you look into one of the several Python address parsing packages.  No need to reinvent a perfectly good wheel.

ARCADMIN
New Contributor II

Thank you I will...for this particular project I just need house number and street name including prefix, name, type and suffix.  Most address parsers I've seen are designed to split all those street name elements out which is usually what you would want but not in this particular case...

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Joshua-  how does one go about installing  usaddress?  I'm out in left field with the whole pip thing....

Joshua Bixby

That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Assuming you have permissions to modify/update the Python environment folders, it is as straightforward as:

C:\>Python27\ArcGIS10.6\Scripts\pip.exe install usaddress
JoeBorgione
MVP Esteemed Contributor

That's cool....  Really cool.....

That should just about do it....
0 Kudos