Skip to content

Instantly share code, notes, and snippets.

@kjaymiller
Last active December 8, 2016 18:01
Show Gist options
  • Save kjaymiller/e8ece38a6265d79837510517a810a80e to your computer and use it in GitHub Desktop.
Save kjaymiller/e8ece38a6265d79837510517a810a80e to your computer and use it in GitHub Desktop.
List the Top 5 States in a csv from Zip_Codes
import csv
import json
from sys import argv
import requests
base_request = 'http://api.zippopotam.us/us/'
# import the zip codes you will be looking for
with open(argv[1], 'r') as f:
reader = csv.reader(f)
zipcodes = [x[6] for x in reader if x[6] not in ('zip', '')]
states = {}
for zip in zipcodes:
request = requests.get(base_request + zip)
if request.ok:
r_list = json.loads(request.text)
state = r_list['places'][0]['state abbreviation']
if state in states.keys():
states[state] += 1
else:
states[state] = 1
top_5 = sorted(states, key=lambda k: states[k], reverse=True)[:5]
print([(x, states[x]) for x in top_5])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment