Skip to content

Instantly share code, notes, and snippets.

@chrislkeller
Created July 18, 2014 17:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chrislkeller/bc9bb417b961666dfaaf to your computer and use it in GitHub Desktop.
Save chrislkeller/bc9bb417b961666dfaaf to your computer and use it in GitHub Desktop.
# Uses wrapper found here: https://github.com/datadesk/python-googlegeocoder
# pip install python-googlegeocoder
from googlegeocoder import GoogleGeocoder
import time
import logging
logging.basicConfig(format='\033[1;36m%(levelname)s:\033[0;37m %(message)s', level=logging.DEBUG)
def create_list_addresses_from_file():
''' convert text file of addresses to a list '''
list_of_addresses = []
address_file = open("addresses.txt","r")
list_of_addresses = address_file.readlines()
address_file.close()
geocode_addresses_from_file(list_of_addresses)
def geocode_addresses_from_file(list_of_addresses):
''' run a list of addresses through the geocoder '''
geocoder = GoogleGeocoder()
f = open("results.txt", "a")
for address in list_of_addresses:
time.sleep(5)
try:
address = address.replace("\n","")
search = geocoder.get(address)
except ValueError:
continue
first_result = search[0]
output = [
first_result.formatted_address.encode('ascii', 'ignore'),
first_result.geometry.location.lat,
first_result.geometry.location.lng,
first_result.geometry.location_type,
first_result.geometry.partial_match
]
address_for_output = '%s\t%s\t%s\t%s\t%s\n' % (output[0], output[1], output[2], output[3], output[4])
print address_for_output
f.write(address_for_output)
f.close()
if __name__ == "__main__":
create_list_addresses_from_file()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment