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!
Solved! Go to Solution.
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])
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
Situs_Addr.split(',')[1].split(' ')[0]
It didn't work.
!Situs_Addr!.split(',')[1].split(' ')[0]
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])
Same, it didn't work.
Could you 'Check the Geoprocessing results window' for more details?
Are any of the records blank or missing the ","?
Yes, some rows are blank.