Last active
August 29, 2015 14:08
-
-
Save achavez/3d683aadf9136e6bc867 to your computer and use it in GitHub Desktop.
Travis County precinct-by-precinct results parser
This file contains 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 csv, sys | |
# This is a quick and dirty solution to walk the file that Travis County distributes | |
# the day after the election and create a new .csv file that has one row per race | |
# per precinct with the winner and his/her/its vote percentage | |
# | |
# CLI usage: python travis-elex-formatter.py input_file.csv output_file.csv | |
last_field = None | |
winner_total_votes = 0 | |
running_vote_total = 0 | |
race_results = [] | |
with open(sys.argv[1], 'rb') as input_file, open(sys.argv[2], 'wb') as output_file: | |
# Open a reader for the input file | |
results = csv.reader(input_file, delimiter=',') | |
# Skip row headers for the input row | |
next(results, None) | |
# And open a write for the output file | |
output = csv.writer(output_file) | |
# Write row headers | |
output.writerow(['Precinct', 'Race', 'Candidate name', 'Vote count', 'Vote share', 'Won', 'Race tooltip']) | |
# Loop over the input, parse & write the new file | |
for row in results: | |
precinct = row[0] | |
candidate_name = row[14] | |
total_votes = int(row[19]) | |
race = row[10] | |
# Generate a field name that is the precinct + the race name; When this | |
# changes, we know we're on a new race | |
field = precinct + race | |
# If we're now in a different race, different precinct then save the winner | |
# before moving on | |
if (field != last_field) and (winner_total_votes != 0): | |
winner_percent_share = float(winner_total_votes) / float(running_vote_total) | |
won_race = winner_percent_share > .5 | |
# Build the tooltip text | |
sorted_races = [] | |
for sorted_race in sorted(race_results, key=lambda k: k['votes'], reverse=True): | |
percent = float(sorted_race['votes']) / float(running_vote_total) * 100 | |
sorted_races.append("%s: %s (%.1f%%)" % (sorted_race['candidate'], sorted_race['votes'], percent)) | |
# Write a new row to the results file | |
output.writerow([ | |
winner_precinct, | |
winner_race, | |
winner_candidate_name, | |
winner_total_votes, | |
winner_percent_share, | |
won_race, | |
"\n".join(sorted_races) | |
]) | |
# Reset variables | |
winner_total_votes = 0 | |
running_vote_total = 0 | |
race_results = [] | |
# If this is the first for this precinct or the current row has a candidate | |
# with a higher total than the last, mark a new winner | |
if total_votes > winner_total_votes: | |
winner_candidate_name = candidate_name | |
winner_total_votes = total_votes | |
winner_precinct = precinct | |
winner_race = race | |
# Add this candidate to the list of results for this race in this precinct | |
race_results.append({ | |
'candidate': candidate_name, | |
'votes': total_votes | |
}) | |
# Keep a running vote total to calculate the percentage down the road | |
running_vote_total = running_vote_total + total_votes | |
# Save this field name for the next row so we can tell then if we're starting | |
# a new one | |
last_field = field | |
print "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment