Skip to content

Instantly share code, notes, and snippets.

Created May 12, 2013 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/00fe0b238b41699a4225 to your computer and use it in GitHub Desktop.
Save anonymous/00fe0b238b41699a4225 to your computer and use it in GitHub Desktop.
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