-
-
Save anonymous/00fe0b238b41699a4225 to your computer and use it in GitHub Desktop.
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
from __future__ import division | |
import csv | |
f = open('SPR.csv', 'r') | |
readlines = f.readlines() | |
result = [] | |
for line in readlines[1:]: | |
splitline = line.split(',') | |
splitline = [x.replace('"', '').replace('\n', '') for x in splitline] | |
d = { | |
'SEAT' : splitline[2], | |
'RegisteredVoters': splitline[3], | |
'voterTurnout': splitline[4], | |
} | |
mapPartyToVotes = dict(zip(range(14, 21), range(21, 28))) | |
for i in range(14,28): | |
if splitline[i].startswith('BN'): | |
d['VotedForWinning'] = splitline[mapPartyToVotes[i]] # note: this list number of votes to BN. | |
break # we're done. get out of loop | |
else: | |
d['VotedForWinning'] = 0 # is this even possible that BN didn't compete? | |
if splitline[2].startswith('P'): | |
d['Type'] = 'P' | |
else: | |
d['Type'] = 'N' | |
d['WinningRatio'] = int(d['VotedForWinning'])/int(d['voterTurnout']) | |
d['TurnoutRatio'] = int(d['voterTurnout'])/int(d['RegisteredVoters']) | |
result.append(d) | |
g = open('SPR_recoded_BN_Winning.csv', 'w') | |
fieldNames = result[0].keys() | |
print fieldNames | |
writer = csv.DictWriter(g, fieldnames=fieldNames) | |
writer.writerow(dict(zip(fieldNames, fieldNames))) | |
for r in result: | |
writer.writerow(r) | |
g.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment