Skip to content

Instantly share code, notes, and snippets.

@kmjones1979
Created June 8, 2016 05:55
Show Gist options
  • Save kmjones1979/da1dfd949de7a900ba56533301394c46 to your computer and use it in GitHub Desktop.
Save kmjones1979/da1dfd949de7a900ba56533301394c46 to your computer and use it in GitHub Desktop.
Python script to parse a CSV for a ZIP code and then query Mapquest's API for details between source and destination
import requests
import csv
# zip code api variables
API_URL = 'http://www.mapquestapi.com/directions/v2/route'
KEY = 'SECRET'
DEST = 'ZIP_CODE_HERE'
# csv variables
FILE = '/path/to/geo_zip.csv'
with open(FILE, 'rb') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['zip'] == "":
print("No Zip","Error")
else:
ORIG = row['zip']
payload = {'from': ORIG,'to': DEST,'key': KEY }
r = requests.get(API_URL, params=payload)
data = r.json()
try:
d = data['route']['distance']
print(ORIG, d)
except KeyError:
d = "Error"
print(ORIG, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment