Skip to content

Instantly share code, notes, and snippets.

@KanishkVashisht
Created March 5, 2018 22:06
Show Gist options
  • Save KanishkVashisht/2e8a10236ae68245fbd77f9ff0d54bcb to your computer and use it in GitHub Desktop.
Save KanishkVashisht/2e8a10236ae68245fbd77f9ff0d54bcb to your computer and use it in GitHub Desktop.
Takes an input longitude and latitude from a csv where lat,long is column 0,1 -> Zip code as an output.
import csv
import requests
import json
#all you need to edit
INPUT_FILE = "";
OUTPUT_FILE = "";
API_KEY = "";
subwayoutput = open(OUTPUT_FILE,'w+')
writer = csv.writer(subwayoutput, delimiter=',',quotechar='|', quoting=csv.QUOTE_MINIMAL)
with open(INPUT_FILE,'r+') as csvfile:
reader = csv.reader(csvfile, delimiter=',',quotechar='|')
next(reader)
for row in reader:
longitude = row[0].strip();
latitude = row[1].strip();
latlng = latitude+","+longitude
latlng.strip()
query = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+latlng+"&key="+API_KEY
r = requests.get(query)
r = r.json()
r = r["results"]
r = r[0]
r = r["address_components"]
r= r[-1]
writer.writerow([latitude, longitude, r["short_name"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment