-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_district_txt.py
More file actions
50 lines (33 loc) · 1.68 KB
/
Copy pathparse_district_txt.py
File metadata and controls
50 lines (33 loc) · 1.68 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
import json
with open('counties.json') as dt:
counties_dict = json.load(dt)
cities = ['Baltimore city', 'Fredericksburg city', 'Newport News city', 'Poquoson city', 'Carson City']
for state in counties_dict:
if state != 'Wyoming' and state != 'Montana' and state != 'Vermont' and state != 'Alaska' and state != 'North Dakota' and state != 'South Dakota' and state != 'District of Columbia' and state != 'Rhode Island':
f = open('Districts/%s.txt' % (state.replace(' ','_')),'rU')
sections = f.read().split('Congressional District')[1:]
print state
for section in sections:
counties = section.split('\n')
district = int(counties[0])
counties = counties[2:]
for c in counties:
print c, state
c_name = (c.split(' (')[0]).decode('latin-1')
if state == 'Louisiana':
c_name += ' Parish'
elif c_name not in cities and 'city' not in c_name:
c_name += ' County'
if state == 'Louisiana' and c_name == 'Jefferson County':
c_name = 'Jefferson Davis Parish'
if c != '':
if type(counties_dict[state][c_name]) != list:
counties_dict[state][c_name] = []
counties_dict[state][c_name].append(int(district))
else:
for c in counties_dict[state]:
counties_dict[state][c] = 1
with open('counties.json','w') as dt:
json.dump(counties_dict,dt)
# Alaska and Delware and Wyoming and Montana are at-large
# Vermont has 1