#Need help splitting out city state and zip code

4 messages · Page 1 of 1 (latest)

round kettle
#

I have this line of code that should extract the city state and zip code. However when it extracts the city its pulling the address in with it.
This is what it looks like printed:
city: 10350 HIGHWAY 188 GRAND BAY

How can I get the address out along with all the white spaces and just keep the city?

Code:

mailing_address_lines = [line.strip() for line in mailing_address.split('\n') if line.strip()]
address = mailing_address_lines[0] # first line is the address
print(f"address: {address}")
city_state_zip_pattern = r'^(.+?),\s+([A-Z]{2})\s+(\d{5}(?:-\d{4})?)$'
city_state_zip_match = re.match(city_state_zip_pattern, mailing_address_lines[-1])
if city_state_zip_match:
city = city_state_zip_match.group(1)
state = city_state_zip_match.group(2)
zip_code = city_state_zip_match.group(3)
else:
city_state_zip = mailing_address_lines[-1]
address_parts = address.split(city_state_zip)
if len(address_parts) > 1:
city = address_parts[1].strip()
else:
city = ""
city_state_zip_parts = city_state_zip.split()
state = city_state_zip_parts[-2]
zip_code = city_state_zip_parts[-1]
address = address_parts[0].strip()
print(f"address: {address}")
print(f"city: {city}")
print(f"state: {state}")
print(f"zip_code: {zip_code}")

rich berry
#

There are a lot of assumptions here, including that the address is in the US, the zipcode is only using Zip and not Zip+4, and so on. Maybe you should be asking ChatGPT this question! 🙂

rich berry
#

Also, the problem here is your data is missing the state and the zip altogether. So the extraction pulled the wrong fields.

past mesa
#

Just put it into chat GPT, tell it what’s going wrong, and ask it to fix it