Skip to content

Instantly share code, notes, and snippets.

@jongamble
Last active December 12, 2015 08:09
Show Gist options
  • Save jongamble/4742061 to your computer and use it in GitHub Desktop.
Save jongamble/4742061 to your computer and use it in GitHub Desktop.
Geocoding addresses from a CSV file and outputting to a json file
#!/usr/bin/python
# Filename: geocoding.py
import csv
from geopy import geocoders
import time
g = geocoders.Google()
spamReader = csv.reader(open('fileName.csv', 'rbU'), quotechar='|')
f = open("data.json", 'w')
f.write('var data = { "count": 13000, "locations": [')
for row in spamReader:
a = ', '.join(row)
time.sleep(.1)
try:
for place, (lat, lng) in g.geocode(a, exactly_one=False):
#print place, lat, lng
continue
except (ValueError, geocoders.google.GQueryError, geocoders.google.GeocoderResultError, geocoders.google.GBadKeyError, geocoders.google.GTooManyQueriesError):
#print("Error: geocode failed on input %s with message %s"%(a, error_message))
continue
b = '{"location_title": ' + '"' + str(place).replace("'", " ") + '"' + ', "longitude": ' + str(lng) + ', "latitude": ' + str(lat) + "},\n"
print b
f.write(b)
f.write(']}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment