Splitting string at "," multiple addresses

3799
13
Jump to solution
07-21-2020 11:30 AM
LABenitez
New Contributor II

I got table field from the county and I just want to extract city from the addresses provided, I don't have any python background.

sample table field situs_addr:

123 street name, city state zip

45 street and street, city state zip

1000 street blvd, city name state zip

I successfully got the address number with street name separated using "split"

!situs_addr!.split(',')[0]

but I'm having a hard time figuring out how to extract just the city, since the length of each addresses are different.

Thank you!

0 Kudos
13 Replies
DavidPike
MVP Frequent Contributor

pre-logic window (python):

def func(string):

   #could make this better but does the job probably
    if len(string) > 0:
        result = (" ".join(((a.split(',')[1]).split(" "))[:-2]))[1:]
    else:
        result = ""

    return result

code window below pre-logic window:

func(!situs_addr!)

JoshuaBixby
MVP Esteemed Contributor

The OP didn't state (no pun intended) whether State is name or postal abbreviation.  It is likely the latter, but if the former, this approach will fail on several states.

0 Kudos
DavidPike
MVP Frequent Contributor

Of course! I even assured myself that all states had one word names.

0 Kudos
LABenitez
New Contributor II

(" ".join(( !Situs_Addr! .split(',')[1]).split(" "))[:-9])

I just selected the one with addresses and this code works!

Thank you so much!

0 Kudos