Created
October 19, 2015 00:04
-
-
Save Roguelazer/3c0600e36731b948296a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python | |
import collections | |
# download files from http://sfgov2.org/index.aspx?page=4767 | |
precinct_map = collections.defaultdict(list) | |
with open('PDMJ001_DistPctExtract.txt', 'r') as dfd: | |
# skip the header | |
next(dfd) | |
for line in dfd: | |
fields = line.rstrip().split('\t') | |
name = fields[2] | |
try: | |
precinct_id = int(fields[3]) | |
precinct_portion = int(fields[4]) if fields[4] else '' | |
precinct_map[(precinct_id, precinct_portion)].append(name) | |
except: | |
print line | |
break | |
data = [] | |
with open('SGMJ001_StreetExtract_20150826.txt', 'r') as sfd: | |
keys = sfd.readline().rstrip().split('\t') | |
StreetData = collections.namedtuple('StreetData', keys) | |
for line in sfd: | |
fields = [ | |
(int(f) if all(x.isdigit() for x in f) and f else f) | |
for f in | |
line.rstrip().split('\t') | |
] | |
datum = StreetData(**dict(zip(keys, fields))) | |
data.append(datum) | |
district_3 = [d for d in data if 'SUP DIST 3' in precinct_map[(d.PrecinctID, d.PrecinctPortion)]] | |
print sum(d.ActiveVoterCount for d in district_3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment