Skip to content

Instantly share code, notes, and snippets.

@jsjohnst
Last active November 12, 2016 04:36

Revisions

  1. jsjohnst revised this gist Nov 12, 2016. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions 2016_election.py
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,9 @@

    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']
    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
  2. jsjohnst revised this gist Nov 12, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 2016_election.py
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@
    'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA',
    'WI', 'WV', 'WY']

    candidate_votes = {'Clinton': 0}
    candidate_votes = {}
    total_votes = 0

    for state in states:
  3. jsjohnst created this gist Nov 12, 2016.
    34 changes: 34 additions & 0 deletions 2016_election.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    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 = {'Clinton': 0}
    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)