Skip to content

Instantly share code, notes, and snippets.

@dwillis
Created December 8, 2020 00:06
Show Gist options
  • Save dwillis/ff11e550811cdeb426e15a7fbe4da70e to your computer and use it in GitHub Desktop.
Save dwillis/ff11e550811cdeb426e15a7fbe4da70e to your computer and use it in GitHub Desktop.
import csv
source = 'greene.txt'
offices = ['PRESIDENT AND VICE PRESIDENT', 'LIEUTENANT GOVERNOR FED-INTRA', 'GOVERNOR FED-INTRA', 'US Representative, District 34', 'SECRETARY OF STATE FED-INTRA', 'STATE TREASURER FED-INTRA',
'ATTORNEY GENERAL FED-INTRA', 'US REPRESENTATIVE - DISTRICT 7 FED-INTRA', 'STATE REPRESENTATIVE - 135 DST 135', 'COMMISSIONER - DISTRICT 2 DST 2', 'SHERIFF GREENE', 'ASSESSOR GREENE',
'TREASURER GREENE', 'PUBLIC ADMINISTRATOR GREENE', 'Constitutional Amendment No. 1 FED-INTRA', 'Constitutional Amendment No. 3 FED-INTRA', 'Supreme Court Judge - Patricia Breckenridge GREENE',
'Court of Appeals Judge, Southern District - Gary W.', '31st Judicial Circuit Div. No. 2 - Jones GREENE', '31st Judicial Circuit Div. No. 6 - Borthwick GREENE', 'Associate - 31st Circuit Div. No. 23 - Hosmer GREENE',
'Associate - 31st Circuit Div. No. 26 - Carrier GREENE', 'STATE REPRESENTATIVE - 134 DST 134', 'STATE REPRESENTATIVE - 132 DST 132', 'COMMISSIONER - DISTRICT 1 DST 1', 'STATE REPRESENTATIVE - 133 DST 133',
'STATE REPRESENTATIVE - 136 DST 136', 'STATE REPRESENTATIVE- 137 DST 137', 'STATE REPRESENTATIVE - 131 DST 131', 'STATE REPRESENTATIVE - DISTRICT 130 DST 130']
lines = open(source).readlines()
results = []
for line in lines:
if line == '\n':
continue
if line == 'DISTRICT\n':
continue
if "<" in line:
continue
if "Official Results" in line:
continue
if "General Election" in line:
continue
if "November 3, 2020" in line:
continue
if "VOTER TURNOUT - BLANK" in line:
continue
if "Report EL30A" in line:
continue
if "Run Date" in line:
precinct = None
continue
if "TOTAL VOTES" in line:
continue
if 'LyncGREENE' in line:
continue
if '(VOTE FOR)' in line:
continue
if 'VOTER TURNOUT - TOTAL' in line:
continue
if any(o in line for o in offices):
office = line.strip()
if not " . " in line and not any(o in line for o in offices) and 'HOWIE HAWKINS' not in line:
precinct = line.strip()
if " . " in line:
if "REGISTERED VOTERS" in line:
office = None
candidate = "Registered Voters"
party = None
votes = line.split('. .', 1)[1].split(' ',1)[1].replace('.','').strip()
election_day, absentee, absentee2 = ["", "", ""]
elif "BALLOTS CAST" in line:
office = None
candidate = "Ballots Cast"
party = None
if len([x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
try:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
except:
fill, votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
elif 'WRITE-IN' in line:
candidate = 'Write-ins'
party = None
if len([x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
try:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
except:
fill, votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
elif 'Total' in line:
continue
elif 'Over Votes' in line:
candidate = 'Over Votes'
party = None
if len([x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
elif 'Under Votes' in line:
candidate = 'Under Votes'
party = None
if len([x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
if '(' in line:
if 'JORGENSEN' in line:
candidate, party = "JO JORGENSEN JEREMY (SPIKE) COHEN", 'LIB'
elif 'KASOFF' in line:
candidate, party = "NICHOLAS (NICK) KASOFF", 'LIB'
elif "JOHN R. (JAY) ASHCROFT" in line:
candidate, party = "JOHN R. (JAY) ASHCROFT", 'REP'
else:
candidate, party = line.split('(', 1)
party = party[0:3]
else:
candidate = line.split(' .')[0]
party = None
candidate = candidate.strip()
try:
if len([x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
try:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split(' . ', 1)[1].split(' ',1)[1].split(' ') if x !='']
except:
fill, votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split(' . ', 1)[1].split(' ',1)[1].split(' ') if x !='']
if votes.replace(',','').replace('. .', '').strip().replace('.','').strip() == '':
votes = '0'
results.append(['Greene', precinct, office, None, party, candidate, votes.replace(',','').replace('. .', '').strip().replace('.','').strip(), election_day.replace(',','').strip(), absentee.replace(',','').strip(), absentee2.replace(',','').strip()])
except:
if len([x.strip() for x in line.split(' . ', 1)[1].split(' ',1)[1].split(' ') if x !='']) == 4:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('. .', 1)[1].split(' ',1)[1].split(' ') if x !='']
else:
try:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split(' . ', 1)[1].split(' ',1)[1].split(' ') if x !='']
except:
fill, votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split(' . ', 1)[1].split(' ',1)[1].split(' ') if x !='']
if votes.replace(',','').replace('. .', '').strip() == '':
votes = '0'
results.append(['Greene', precinct, office, None, party, candidate, votes.replace(',','').strip(), election_day.replace(',','').strip(), absentee.replace(',','').strip(), absentee2.replace(',','').strip()])
elif 'HOWIE HAWKINS' in line:
office = 'PRESIDENT AND VICE PRESIDENT'
district = None
candidate = line.split('(')[0].strip()
party = 'GRN'
try:
votes, election_day, absentee, absentee2 = [x.strip() for x in line.split('(GR')[1].split(' ',1)[1].split(' ') if x !='']
except:
votes, pct, election_day, absentee, absentee2 = [x.strip() for x in line.split('(GR')[1].split(' ',1)[1].split(' ') if x !='']
results.append(['Greene', precinct, office, None, party, candidate, votes.replace(',','').strip(), election_day.replace(',','').strip(), absentee.replace(',','').strip(), absentee2.replace(',','').strip()])
with open('20201103__mo__general__greene__precinct.csv', 'wt') as csvfile:
w = csv.writer(csvfile)
headers = ['county', 'precinct', 'office', 'district', 'party', 'candidate', 'votes', 'election_day', 'absentee', 'absentee2']
w.writerow(headers)
w.writerows(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment