Created
June 8, 2016 05:55
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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