-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseEC.py
More file actions
executable file
·123 lines (106 loc) · 3.87 KB
/
parseEC.py
File metadata and controls
executable file
·123 lines (106 loc) · 3.87 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
import re
import sys
def toabbrev(x):
return mapping[x]
def identity(x):
return x
def isabbrev(x):
return len(mapping[x] if int(year) > 1960 and x in mapping else x) == 2
def isdigit(x):
pattern = re.compile('^\d+$')
return pattern.search(x) is not None
def makedict(states, statevals):
statecounts = list(filter(isdigit, statevals.rstrip('\n').split(',')))
lastval = statecounts.pop()
print("got {0} values: {1}".format(len(statecounts), ','.join(statecounts)))
return lastval, dict(zip(states, statecounts))
file = sys.argv[1]
year = file.split('/').pop().split('.')[0]
with open(file, 'r') as f:
lines = f.read().splitlines()
# for all lines
mapping = {
'Alabama': 'AL',
'Alaska': 'AK',
'Arizona': 'AZ',
'Arkansas': 'AR',
'California': 'CA',
'Colorado': 'CO',
'Connecticut': 'CT',
'Delaware': 'DE',
'District of Columbia': 'DC',
'Florida': 'FL',
'Georgia': 'GA',
'Hawaii': 'HI',
'Idaho': 'ID',
'Illinois': 'IL',
'Indiana': 'IN',
'Iowa': 'IA',
'Kansas': 'KS',
'Kentucky': 'KY',
'Louisiana': 'LA',
'Maine': 'ME',
'Maryland': 'MD',
'Massachusetts': 'MA',
'Michigan': 'MI',
'Minnesota': 'MN',
'Mississippi': 'MS',
'Missouri': 'MO',
'Montana': 'MT',
'Nebraska': 'NE',
'Nevada': 'NV',
'New Hampshire': 'NH',
'New Jersey': 'NJ',
'New Mexico': 'NM',
'New York': 'NY',
'North Carolina': 'NC',
'North Dakota': 'ND',
'Ohio': 'OH',
'Oklahoma': 'OK',
'Oregon': 'OR',
'Pennsylvania': 'PA',
'Rhode Island': 'RI',
'South Carolina': 'SC',
'South Dakota': 'SD',
'Tennessee': 'TN',
'Texas': 'TX',
'Utah': 'UT',
'Vermont': 'VT',
'Virginia': 'VA',
'Washington': 'WA',
'West Virginia': 'WV',
'Wisconsin': 'WI',
'Wyoming' : 'WY'
}
states = []
counts = []
ecDict = {}
total = 0
baseYear = 1789
isData = False
for line in lines:
try:
totPos = line.index('Total')
if totPos > 0:
states = list(map(toabbrev if int(year) > 1960 else identity, filter(isabbrev, line.rstrip('\n').split(','))))
# print("got {0} states: {1}".format(len(states), ','.join(states)))
elif totPos == 0:
if line.endswith('Total'):
isData = True
else:
(total, ecDict) = makedict(states, line)
except ValueError:
if int(year) > 1960:
if re.compile('Electoral Vote').search(line) is not None:
(total, ecDict) = makedict(states, line)
break
if isData:
(total, ecDict) = makedict(states, line)
break
else:
print(".", end='')
term = int((int(year) - baseYear + 1)/4) + 1
for k, v in ecDict.items():
print("{0},{1},{2},{3},{4},{5}".format(year, int(year) + 1, int(year) + 5, term, k, v))
# print("total {0} for {1}".format(total, year))