Splitting string at "," multiple addresses

1264
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
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

AS the city could be more than 1 word I might suggest splitting by a space, then takin the last but 2 entries:

(" ".join((!situs_addr!.split(',')[1]).split(" "))[:-2])

View solution in original post

13 Replies
DonaldShaw2
Esri Contributor

Lets say you have a variable called address

address = '123 street name, city state zip'

You are correctly splitting the string on the comma. When you do that, you get this


['123 street name', 'city state zip']

This gives you two items in a list. You want to grab the second item (index 1), and split again... but this time splitting on spaces. You will then grab the city on index 0 (state = index 1, zip = index 2).

address.split(', ')[1].split(' ')[0]

The result is the city

LABenitez
New Contributor II

Situs_Addr.split(',')[1].split(' ')[0]

It didn't work.

0 Kudos
LABenitez
New Contributor II

!Situs_Addr!.split(',')[1].split(' ')[0]

0 Kudos
DavidPike
MVP Frequent Contributor

AS the city could be more than 1 word I might suggest splitting by a space, then takin the last but 2 entries:

(" ".join((!situs_addr!.split(',')[1]).split(" "))[:-2])

View solution in original post

LABenitez
New Contributor II

Same, it didn't work.

0 Kudos
DavidPike
MVP Frequent Contributor

Could you 'Check the Geoprocessing results window' for more details?

0 Kudos
LABenitez
New Contributor II

0 Kudos
KenBuja
MVP Honored Contributor

Are any of the records blank or missing the ","?

0 Kudos
LABenitez
New Contributor II

Yes, some rows are blank. 

0 Kudos