diff --git a/elex1/election_results.py b/elex1/election_results.py index b03b7f5..d119dd4 100644 --- a/elex1/election_results.py +++ b/elex1/election_results.py @@ -15,7 +15,7 @@ """ import csv -import urllib +import urllib.request as urllib from operator import itemgetter from collections import defaultdict from os.path import dirname, join @@ -27,7 +27,7 @@ urllib.urlretrieve(url, filename) # Create reader for ingesting CSV as array of dicts -reader = csv.DictReader(open(filename, 'rb')) +reader = csv.DictReader(open(filename, 'r')) # Use defaultdict to automatically create non-existent keys with an empty dictionary as the default value. # See https://pydocs2cn.readthedocs.org/en/latest/library/collections.html#defaultdict-objects @@ -85,7 +85,7 @@ first['winner'] = 'X' # Get race metadata from one set of results - result = cand_results.values()[0][0] + result = list(cand_results.values())[0][0] # Add results to output summary[race_key] = { 'date': result['date'], @@ -98,7 +98,7 @@ # Write CSV of results outfile = join(dirname(__file__), 'summary_results.csv') -with open(outfile, 'wb') as fh: +with open(outfile, 'w') as fh: # We'll limit the output to cleanly parsed, standardized values fieldnames = [ 'date', diff --git a/elex2/election_results.py b/elex2/election_results.py index 1d4bb91..c347c5a 100644 --- a/elex2/election_results.py +++ b/elex2/election_results.py @@ -12,7 +12,7 @@ """ import csv -import urllib +import urllib.request from operator import itemgetter from collections import defaultdict from os.path import dirname, join @@ -34,7 +34,7 @@ def main(): def download_results(path): """Download CSV of fake Virginia election results from GDocs""" url = "https://docs.google.com/spreadsheet/pub?key=0AhhC0IWaObRqdGFkUW1kUmp2ZlZjUjdTYV9lNFJ5RHc&output=csv" - urllib.urlretrieve(url, path) + urllib.request.urlretrieve(url, path) def parse_and_clean(path): """Parse downloaded results file and perform various data clean-ups @@ -47,7 +47,7 @@ def parse_and_clean(path): """ # Create reader for ingesting CSV as array of dicts - reader = csv.DictReader(open(path, 'rb')) + reader = csv.DictReader(open(path, 'r')) # Use defaultdict to automatically create non-existent keys with an empty dictionary as the default value. # See https://pydocs2cn.readthedocs.org/en/latest/library/collections.html#defaultdict-objects @@ -114,7 +114,7 @@ def summarize(results): first['winner'] = 'X' # Get race metadata from one set of results - result = cand_results.values()[0][0] + result = list(cand_results.values())[0][0] # Add results to output summary[race_key] = { 'all_votes': all_votes, @@ -135,7 +135,7 @@ def write_csv(summary): """ outfile = join(dirname((__file__)), 'summary_results.csv') - with open(outfile, 'wb') as fh: + with open(outfile, 'w') as fh: # Limit output to cleanly parsed, standardized values fieldnames = [ 'date',