Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split Address String in Python?

I’m reading data from excel trying to split this address string into variables but I cant split the Apartment # from the city and help?

When I read from my excel file I get this strAddress = row[11] in Python:

5555 Fat Drive
# 6
Broken Bow, OK   74728
5555 Farmers Mill Rd
Garvin, OK   74736

When I split this with a comma strAddress.split(',') I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

['5555 Fat Drive\n# 6\nBroken Bow', ' OK   74728']
['5555 Farmer Mill Rd\nGarvin', ' OK   74736']

When I split strAddr1 = strAddress.split('\n')[0] I get:

5555 Fat Drive
5555 Farmer Mill Rd

I want to be able to get the second address such as #6 without capturing the city name any suggestions?

>Solution :

Split the address into lines. If the address has an apartment number, there will be 3 lines, and the apartment will be in the second line.

lines = strAddress.split('\n')
if len(lines) == 3:
    apartment = lines[1]
else:
    apartment = ''
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading