AnsweredAssumed Answered

"List index out of range" error when parsing an address field.

Question asked by jthorup on Apr 11, 2018
Latest reply on Apr 12, 2018 by jthorup

I'm getting a "list index out of range" error and I don't know why.  I'm parsing an address field to return only the street name.  The following line gives the error:


elif prop_location.strip().split()[1] == 'S' or 'W':

    return ' '.join(prop_location.strip().split()[2:-1]


According to the error log, the issue seems to be with the elif statement.  This code has worked fine for a long time, but the last several times I've run it it has given me this error.  I have checked the feature class for addresses that have a W or S with an index greater than 1, but I haven't found any.  There are some records that are blank, and others without a 'W' or 'S', but they shouldn't be a part of this if statement.  I'm not sure how to fix the code.  Here is the all the code for context (problem statement is line 12).  The error notification is added as an attachment:


def getStreetName( prop_location, Pre_Dir ):
    if prop_location is None:
        return None
    elif prop_location == ' ':
        return None
    elif " #" in prop_location:
        head, sep, tail = prop_location.partition(' #')
        if head.strip().split()[1] == 'S' or 'W':
            return ' '.join(head.strip().split()[2:-1])
            return ' '.join(head.strip().split()[1:-1])
    elif prop_location.strip().split()[1] == 'S' or 'W':
        return ' '.join(prop_location.strip().split()[2:-1])
        return ' '.join(prop_location.strip().split()[1:-1])