Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Last active November 12, 2016 04:36
Show Gist options
  • Save jsjohnst/a402db711d495824ba4741b9f7bcb90b to your computer and use it in GitHub Desktop.
Save jsjohnst/a402db711d495824ba4741b9f7bcb90b to your computer and use it in GitHub Desktop.
import urllib
import json
import locale
locale.setlocale(locale.LC_ALL, 'en_US')
state = "UT"
states = ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS',
'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV',
'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY']
candidate_votes = {}
total_votes = 0
for state in states:
url = "http://data.cnn.com/ELECTION/2016/%s/county/P_county.json" % state
response = urllib.urlopen(url)
data = json.loads(response.read())
for county in data['counties']:
for candidate in county['race']['candidates']:
votes = int(candidate['votes'])
name = candidate['lname']
if not name in candidate_votes:
candidate_votes[name] = 0
candidate_votes[name] += votes
total_votes += votes
for candidate, votes in candidate_votes.iteritems():
print "%s: %s (%.2f%%)" % (candidate, locale.format("%d", votes, grouping=True), float(votes) / total_votes * 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment