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}")