Last active
June 8, 2018 03:44
-
-
Save danasf/ab9211908bb51be4e37ef55ce2ac6e72 to your computer and use it in GitHub Desktop.
ballot images to CSV
This file contains hidden or 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
# converts ballot images from sf elections into a csv you can easily import into your database/spreadsheet | |
# this is released into the public domain. dana sniezko, 2018 | |
# download the text file from https://sfelections.sfgov.org/june-5-2018-election-results-detailed-reports | |
ballot_image_file = "20180606_ballotimage.txt" | |
# what you want to name your output csv | |
output_file_name = "parsed_ballots.csv" | |
# from http://www.sfelections.org/results/20180605/data/20180605_4/20180605_4_masterlookup.txt | |
# dictionary of to precincts | |
precincts = {"0000001":"1101","0000002":"1102","0000003":"1103","0000004":"1104/1105","0000005":"1106","0000006":"1107","0000007":"1108","0000008":"1109","0000009":"1111","0000010":"1112","0000011":"1113","0000012":"1114","0000013":"1115","0000014":"1116","0000015":"1117","0000016":"1118","0000017":"1119","0000018":"1121","0000019":"1122","0000020":"1123","0000021":"1124","0000022":"1125","0000023":"1126","0000024":"1127","0000025":"1128","0000026":"1129","0000027":"1131","0000028":"1132","0000029":"1133","0000030":"1134","0000031":"1135","0000032":"1136","0000033":"1141","0000034":"1142","0000035":"1143","0000036":"1144","0000037":"1145","0000038":"1146","0000039":"1147","0000040":"1148","0000041":"1149","0000042":"1151","0000043":"1152","0000044":"1153","0000045":"1154","0000046":"7001","0000047":"7002","0000048":"7003","0000049":"7004","0000050":"7005","0000051":"7006","0000052":"7007","0000053":"7008","0000054":"7009","0000055":"7011","0000056":"7012","0000057":"7013","0000058":"7014","0000059":"7015","0000060":"7016","0000061":"7017","0000062":"7018","0000063":"7019","0000064":"7021","0000065":"7022","0000066":"7023","0000067":"7024","0000068":"7025","0000069":"7026","0000070":"7027","0000071":"7028","0000072":"7029","0000073":"7030","0000074":"7031","0000075":"7032","0000076":"7033","0000077":"7034","0000078":"7035","0000079":"7036","0000080":"7037","0000081":"7038","0000082":"7039","0000083":"7041","0000084":"7042","0000085":"7043","0000086":"7044","0000087":"7045","0000088":"7046","0000089":"7047","0000090":"7048","0000091":"7049","0000092":"7051","0000093":"7052","0000094":"7053","0000095":"7054","0000096":"7055","0000097":"7101","0000098":"7201","0000099":"7202","0000100":"7203","0000101":"7204","0000102":"7205","0000103":"7206","0000104":"7207","0000105":"7208","0000106":"7209","0000107":"7301","0000108":"7302","0000109":"7303","0000110":"7304","0000111":"7305","0000112":"7306","0000113":"7307","0000114":"7308","0000115":"7309","0000116":"7311","0000117":"7312","0000118":"7313","0000119":"7314","0000120":"7315","0000121":"7316","0000122":"7317","0000123":"7318","0000124":"7319","0000125":"7321","0000126":"7322","0000127":"7323","0000128":"7324","0000129":"7325","0000130":"7326","0000131":"7327","0000132":"7328","0000133":"7329","0000134":"7331","0000135":"7332","0000136":"7333","0000137":"7334","0000138":"7335","0000139":"7336","0000140":"7337","0000141":"7338","0000142":"7339","0000143":"7341","0000144":"7342","0000145":"7343","0000146":"7344","0000147":"7345","0000148":"7346","0000149":"7347","0000150":"7348","0000151":"7349","0000152":"7501","0000153":"7502","0000154":"7503","0000155":"7504","0000156":"7505","0000157":"7506","0000158":"7507","0000159":"7508","0000160":"7509/7511","0000161":"7512","0000162":"7513","0000163":"7514","0000164":"7515","0000165":"7516","0000166":"7517","0000167":"7518","0000168":"7519","0000169":"7521","0000170":"7522","0000171":"7523","0000172":"7524","0000173":"7525","0000174":"7526","0000175":"7527/7528","0000176":"7529","0000177":"7531","0000178":"7532","0000179":"7533","0000180":"7534","0000181":"7535","0000182":"7536","0000183":"7537","0000184":"7538","0000185":"7539","0000186":"7541","0000187":"7542","0000188":"7543","0000189":"7544","0000190":"7545","0000191":"7546","0000192":"7547","0000193":"7548","0000194":"7549","0000195":"7551","0000196":"7552","0000197":"7553","0000198":"7554","0000199":"7555","0000200":"7556/7557","0000201":"7601","0000202":"7602","0000203":"7603","0000204":"7604","0000205":"7605","0000206":"7606","0000207":"7607","0000208":"7608","0000209":"7609","0000210":"7611","0000211":"7612","0000212":"7613","0000213":"7614","0000214":"7615","0000215":"7616","0000216":"7617","0000217":"7618","0000218":"7619","0000219":"7621","0000220":"7622","0000221":"7623","0000222":"7624","0000223":"7625","0000224":"7626","0000225":"7627","0000226":"7628","0000227":"7629","0000228":"7631","0000229":"7632","0000230":"7633","0000231":"7634","0000232":"7635","0000233":"7636","0000234":"7637","0000235":"7638","0000236":"7639","0000237":"7641","0000238":"7642","0000239":"7643","0000240":"7644","0000241":"7645","0000242":"7646","0000243":"7647","0000244":"7648","0000245":"7649","0000246":"7651","0000247":"7652","0000248":"7653","0000249":"7701","0000250":"7702","0000251":"7703","0000252":"7704","0000253":"7705","0000254":"7706","0000255":"7707","0000256":"7708","0000257":"7709","0000258":"7711","0000259":"7712","0000260":"7801","0000261":"7802","0000262":"7803","0000263":"7804","0000264":"7805","0000265":"7806","0000266":"7807","0000267":"7808","0000268":"7809","0000269":"7811","0000270":"7812","0000271":"7813","0000272":"7814","0000273":"7815","0000274":"7816","0000275":"7817","0000276":"7818","0000277":"7819","0000278":"7821","0000279":"7822","0000280":"7823","0000281":"7824","0000282":"7825","0000283":"7826","0000284":"7827","0000285":"7828","0000286":"7829","0000287":"7831","0000288":"7832","0000289":"7833","0000290":"7834","0000291":"7835","0000292":"7836","0000293":"7837","0000294":"7838","0000295":"7839","0000296":"7841","0000297":"7842","0000298":"7843","0000299":"7844","0000300":"7845","0000301":"7846","0000302":"7847","0000303":"7848","0000304":"7849","0000305":"7851","0000306":"7852","0000307":"7853","0000308":"7854","0000309":"7855","0000310":"7856","0000311":"7857","0000312":"7858","0000313":"7859","0000314":"7861","0000315":"7862","0000316":"7863","0000317":"7864","0000318":"7865","0000319":"7866","0000320":"7867","0000321":"7868","0000322":"7869","0000323":"7871","0000324":"7872","0000325":"7873","0000326":"7874","0000327":"7875","0000328":"7876","0000329":"7901","0000330":"7902","0000331":"7903","0000332":"7904","0000333":"7905","0000334":"7906","0000335":"7907","0000336":"7908","0000337":"7909","0000338":"7911","0000339":"7912","0000340":"7913","0000341":"7914","0000342":"7915","0000343":"7916","0000344":"7917","0000345":"7918","0000346":"7919","0000347":"7921","0000348":"7922","0000349":"7923","0000350":"7924","0000351":"7925","0000352":"7926","0000353":"7927","0000354":"7928","0000355":"7929","0000356":"7931","0000357":"7932","0000358":"7933","0000359":"7934","0000360":"7935","0000361":"7936","0000362":"7937","0000363":"7938","0000364":"7939","0000365":"7941","0000366":"7942","0000367":"7943","0000368":"7944","0000369":"7945","0000370":"7946","0000371":"7947","0000372":"7948","0000373":"7949","0000374":"7951","0000375":"7952","0000376":"7953","0000377":"7954","0000378":"7955","0000379":"7956","0000380":"7957","0000381":"7958","0000382":"7959","0000383":"9001","0000384":"9101","0000385":"9102","0000386":"9103","0000387":"9104","0000388":"9105","0000389":"9106","0000390":"9107","0000391":"9108","0000392":"9109","0000393":"9111","0000394":"9112","0000395":"9113","0000396":"9114","0000397":"9115","0000398":"9116","0000399":"9117","0000400":"9118","0000401":"9119","0000402":"9121","0000403":"9122","0000404":"9123","0000405":"9124","0000406":"9125","0000407":"9126","0000408":"9127","0000409":"9128","0000410":"9129","0000411":"9131","0000412":"9132","0000413":"9133","0000414":"9134","0000415":"9135","0000416":"9136","0000417":"9137","0000418":"9138","0000419":"9139","0000420":"9141","0000421":"9142","0000422":"9143","0000423":"9144","0000424":"9145","0000425":"9146","0000426":"9147","0000427":"9148","0000428":"9149","0000429":"9151","0000430":"9152","0000431":"9201","0000432":"9202","0000433":"9203","0000434":"9204","0000435":"9205","0000436":"9206","0000437":"9207","0000438":"9208","0000439":"9209","0000440":"9211","0000441":"9212","0000442":"9213","0000443":"9214","0000444":"9215","0000445":"9216","0000446":"9217","0000447":"9218","0000448":"9219","0000449":"9221","0000450":"9222","0000451":"9223","0000452":"9224","0000453":"9225","0000454":"9226","0000455":"9227","0000456":"9228","0000457":"9229","0000458":"9231","0000459":"9232","0000460":"9233","0000461":"9234","0000462":"9235","0000463":"9236","0000464":"9237","0000465":"9238","0000466":"9239","0000467":"9241","0000468":"9242","0000469":"9243","0000470":"9244","0000471":"9245","0000472":"9246","0000473":"9247","0000474":"9248","0000475":"9249","0000476":"9251","0000477":"9252","0000478":"9253","0000479":"9254","0000480":"9255","0000481":"9256","0000482":"9257","0000483":"9258","0000484":"9401","0000485":"9402","0000486":"9403","0000487":"9404","0000488":"9405","0000489":"9406","0000490":"9407","0000491":"9408","0000492":"9409","0000493":"9411","0000494":"9412","0000495":"9413","0000496":"9414","0000497":"9415","0000498":"9416","0000499":"9417","0000500":"9418","0000501":"9419","0000502":"9421","0000503":"9422","0000504":"9423","0000505":"9424","0000506":"9425","0000507":"9426","0000508":"9427","0000509":"9428","0000510":"9429","0000511":"9431","0000512":"9432","0000513":"9433","0000514":"9434","0000515":"9435","0000516":"9436","0000517":"9437","0000518":"9438","0000519":"9439","0000520":"9441","0000521":"9442","0000522":"9443","0000523":"9444","0000524":"9445","0000525":"9446","0000526":"9447","0000527":"9448","0000528":"9449","0000529":"9451","0000530":"9452","0000531":"9453","0000532":"9501","0000533":"9502","0000534":"9503/9504","0000535":"9505","0000536":"9506","0000537":"9507","0000538":"9508","0000539":"9509","0000540":"9511","0000541":"9512","0000542":"9513","0000543":"9514/9522","0000544":"9515","0000545":"9516","0000546":"9517","0000547":"9518","0000548":"9519","0000549":"9521","0000550":"9701","0000551":"9702","0000552":"9703","0000553":"9704","0000554":"9705","0000555":"9706","0000556":"9707","0000557":"9708","0000558":"9709","0000559":"9711","0000560":"9712","0000561":"9713","0000562":"9714","0000563":"9715","0000564":"9716","0000565":"9717","0000566":"9718","0000567":"9719","0000568":"9721","0000569":"9722","0000570":"9723","0000571":"9724","0000572":"9725","0000573":"9726","0000574":"9727","0000575":"9728","0000576":"9729","0000577":"9731","0000578":"9732","0000579":"9733","0000580":"9734","0000581":"9735","0000582":"9736","0000583":"9737","0000584":"9738","0000585":"9739","0000586":"9741","0000587":"9742","0000588":"9743","0000589":"9744","0000590":"9745","0000591":"9746","0000592":"9747","0000593":"9748","0000594":"9749","0000595":"9751","0000596":"9752","0000597":"9753","0000598":"9754","0000599":"9755","0000600":"9756","0000601":"9801","0000602":"9900","0000603":"9901","0000604":"9902"} | |
# dictionary of candidates | |
candidates = { "0000178" : "MICHELLE BRAVO", | |
"0000186" : "JEFF SHEEHY", | |
"0000179" : "LONDON BREED", | |
"0000187" : "LAWRENCE STARK DAGESSE", | |
"0000180" : "MARK LENO", | |
"0000188" : "RAFAEL MANDELMAN", | |
"0000021" : "WRITE-IN", | |
"0000181" : "JANE KIM", | |
"0000182" : "RICHIE GREENBERG", | |
"0000183" : "ANGELA ALIOTO", | |
"0000184" : "AMY FARAH WEISS", | |
"0000185" : "ELLEN LEE ZHOU", | |
"0000020" : "WRITE-IN", | |
"0000200" : "WRITE-IN ANTOINE R. ROGERS", | |
"0000000":"NULL" } | |
# open a file to write out to and write a header | |
print("Opening output file...") | |
output = open(output_file_name, 'w') | |
output.write("contest_id,voter_id,rank,candidate_id,candidate_name,overvote,undervote,tally_type,precinct\n") | |
# open a ballot image file from SF elections | |
with open(ballot_image_file) as f: | |
print("Reading ballot images...") | |
lines = f.readlines() | |
print("Processing...") | |
for line in lines: | |
# from http://www.sfelections.org/results/20180605/data/BallotImageRCVhelp.pdf | |
contest_id = line[0:7] # contest, mayor/supe | |
voter_id = line[7:16] # voter id | |
serial_no = line[16:23] # serial | |
tally_type = line[23:26] # type of tally | |
precinct_id = line[26:33] # precinct number | |
rank = line[35] # rank | |
candidate = line[36:43] # candidate | |
overvote = line[43] # over | |
undervote = line[44] # under | |
# write out your line | |
output.write(contest_id+","+voter_id+","+rank+","+candidate+","+candidates[candidate]+","+overvote+","+undervote+","+tally_type+","+precincts[precinct_id]+"\n") | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment