Split address string field

739
1
05-22-2012 07:31 PM
RobKay
by
New Contributor II
Hi Everyone,
I've got a street address field with the suburb name attached.  I need some help writing a script that splits off the suburb then writes the address and suburb to seperate fields.
eg.  13 Kent Street Gordon
I want the field split like this: Address (field name): 13 Kent Street          Suburb (field name): Gordon
Not all the suburbs are single words so I can't just split off the last word.  The only way I think it may work is to search for the words 'Street, Road, Avenue, Lane, etc. (I've made a comprehensive list) then split the string after these words once they are found.
Is this possible?
Cheers
Rob
Tags (2)
0 Kudos
1 Reply
markdenil
Occasional Contributor III
This gives you a list of lists, each of the inner ones has the street part and the suburb part

streetList = [
    "13 Kent Street Gordon",
    "22 King Road Soho",
    "123 Kong Lane Foggy Bottom",
    "76 Inglis Avenue Don't Go Here",
    "300 4th Street Adams Morgan"]

kindList = ["Street", "Road", "Lane", "Avenue", "Trail"]

newList = []

for street in streetList:
    for kind in kindList:
        if kind in street:
            where = street.find(kind)
            kindLen = len(kind)
            div = where + kindLen
            streetPart = street[:div]
            suburbPart = street[div:]
            newList.append([streetPart, suburbPart])
print newList


The output is:
[['13 Kent Street', ' Gordon'], ['22 King Road', ' Soho'], ['123 Kong Lane', ' Foggy Bottom'],
['76 Inglis Avenue', " Don't Go Here"], ['300 4th Street', ' Adams Morgan']]

and you can use this output in a cursor to set the divided fields.
0 Kudos