-
Notifications
You must be signed in to change notification settings - Fork 11
/
county_converter.py
70 lines (66 loc) · 3.96 KB
/
county_converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import csv
from districts import load_districts
COUNTIES = ['Adams','Allen','Ashland','Ashtabula','Athens','Auglaize','Belmont','Brown','Butler','Carroll','Champaign','Clark','Clermont','Clinton','Columbiana','Coshocton','Crawford','Cuyahoga','Darke','Defiance','Delaware','Erie','Fairfield','Fayette','Franklin','Fulton','Gallia','Geauga','Greene','Guernsey','Hamilton','Hancock','Hardin','Harrison','Henry','Highland','Hocking','Holmes','Huron','Jackson','Jefferson','Knox','Lake','Lawrence','Licking','Logan','Lorain','Lucas','Madison','Mahoning','Marion','Medina','Meigs','Mercer','Miami','Monroe','Montgomery','Morgan','Morrow','Muskingum','Noble','Ottawa','Paulding','Perry','Pickaway','Pike','Portage','Preble','Putnam','Richland','Ross','Sandusky','Scioto','Seneca','Shelby','Stark','Summit','Trumbull','Tuscarawas','Union','Van Wert','Vinton','Warren','Washington','Wayne','Williams','Wood','Wyandot']
districts = load_districts()
with open('/Users/dwillis/Downloads/statewideresultsbycounty.csv', 'r') as csvfile:
filename = "20201103__oh__general__county.csv"
reader = csv.reader(csvfile)
offices = next(reader)
fixed_offices = []
for office in offices[6:]:
if office != '':
o = office.strip()
fixed_offices.append(office.strip())
else:
fixed_offices.append(o)
headers = next(reader)
fixed_cols = headers[0:6]
fixed_cols.extend(['office', 'district', 'party', 'candidate', 'votes'])
cands = headers[6:]
l = list(reader)
results = []
for county in COUNTIES:
county_districts = [d for d in districts if d['county'] == county]
rows = [x for x in l if x[0] == county]
for row in rows:
county = row[0].strip()
for idx, cand in enumerate(cands):
if '(R)' in cand:
party = 'R'
elif '(D)' in cand:
party = 'D'
elif '(G)' in cand:
party = 'G'
elif '(WI)' in cand:
party = None
else:
party = 'I'
if row[0] == 'Percentage' or row[0] == 'Total':
continue
office = fixed_offices[idx]
if 'District' in office:
office, district = office.split(' - District ')
office = office.strip()
else:
district = None
votes = row[idx+6]
if office == 'President (district)' or office == 'Representative to Congress':
d = [int(x['district']) for x in county_districts if x['office'] == 'U.S. House']
if int(district[0:2]) in d:
results.append([county, row[1], row[2], row[3], row[4], office, district, party, cand.split(' (')[0].replace(' ', ' '), votes])
elif office == 'State Representative':
d = [int(x['district']) for x in county_districts if x['office'] == 'State House']
if int(district[0:2]) in d:
results.append([county, row[1], row[2], row[3], row[4], office, district, party, cand.split(' (')[0].replace(' ', ' '), votes])
elif office == 'State Senator':
d = [int(x['district']) for x in county_districts if x['office'] == 'State Senate']
if int(district[0:2]) in d:
results.append([county, row[1], row[2], row[3], row[4], office, district, party, cand.split(' (')[0].replace(' ', ' '), votes])
elif office == 'President' or office == 'U.S. Senator':
results.append([county, row[1], row[2], row[3], row[4], office, district, party, cand.split(' (')[0].replace(' ', ' '), votes])
else:
continue
with open(filename, 'wt') as outfile:
writer = csv.writer(outfile)
writer.writerow(fixed_cols)
writer.writerows(results)