Skip to content

Instantly share code, notes, and snippets.

@conorsg
Created October 1, 2014 21:30
Show Gist options
  • Save conorsg/a884a299416eb3aad174 to your computer and use it in GitHub Desktop.
Save conorsg/a884a299416eb3aad174 to your computer and use it in GitHub Desktop.
makes calls to Google's geocode API down a column of addresses in a csv, writes the lat long on back
#!/usr/bin/env python
import json
import urllib2
import csv
api_key = 'YOUR_API_KEY'
with open('FILE.csv', 'rU') as f:
reader = csv.reader(f)
loc_list = []
for row in reader:
locations = []
row = row[0].replace(' ', '+')
response = json.load(urllib2.urlopen('https://maps.googleapis.com/maps/api/geocode/json?address=' + row + '+New+Orleans,+LA&key=' + api_key))
if response['status'] == 'OK':
lat = response['results'][0]['geometry']['location']['lat']
long = response['results'][0]['geometry']['location']['lng']
locations.append(row)
locations.append(lat)
locations.append(long)
loc_list.append(locations)
else:
locations.append(row)
locations.append(response['status'])
loc_list.append(locations)
output = open('OUTPUT.csv', 'wb')
writer = csv.writer(output)
writer.writerows(loc_list)
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment