'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:

['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 1:[1]

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 = ''

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1