'Find address using latitude/ longitude attribute (Python & geopy)

I have been trying to resolve a particular problem - extract location/address out of the predefined list. The list represents e-mail message where address will definitely be. I have not found any efficient way how to do it, however, I was able to come with some idea.

I might go through the message using for loop and state, whether the sequence of words has attribute of longitude/latitude. If not, the word will be deleted, if yes, the word will remain in the new list and loop will go through next words.

This way I can distinguish which words are address and I can surely extract the address. Do you think this way might be efficient? If not, is there any more complex/better way how to extract the address out of a string?

For example:

email = "Hello, I am looking forward to your letter, please send it on 20 W 34th St, New York, NY 10001, USA"

I want to extract:

adress = "20 W 34th St, New York, NY 10001, USA"

I have this piece of code:

email = email.split()
from geopy.geocoders import Nominatim
adress_scan = []
geolocator = Nominatim(user_agent = "sth", timeout = 10)

for i in adresa:
    adresa_scan.append(i)
    location = geolocator.geocode(str(adresa_scan))
    if hasattr(adresa_scan, 'longitude') == False:
        adresa_scan.remove(i)   
print(adresa_scan) 

The program keeps printing empty list and I don't know why. Thank you, I appreciate any help:)



Sources

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

Source: Stack Overflow

Solution Source