-
Notifications
You must be signed in to change notification settings - Fork 7
/
process.py
74 lines (59 loc) · 2.06 KB
/
process.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
71
72
73
74
# Requirements: Python 2.6 or greater
import csv, sys, json, time, copy
from itertools import groupby
#Process census file
#*********************************
tracts = csv.DictReader(open('data/csv/2010_relationship.csv', 'rb'), delimiter = ',', quotechar = '"')
tracts_sort = sorted(tracts, key = lambda x: x['GEOID10'])
changed_tracts = csv.DictReader(open('data/csv/2010_substantial_changes.csv', 'rb'), delimiter = ',', quotechar = '"')
changed_sort = sorted(changed_tracts, key = lambda x: x['GEOID10'])
# Define functions
final = {}
final['2000'] = {}
final['2010'] = {}
changedIDs = []
def parseData():
row_count = 0
for t in tracts_sort:
row_count +=1
changes = {}
changes['2000'] = {}
changes['2000']['geoid'] = t['GEOID00']
changes['2000']['part'] = t['PART10']
changes['2000']['poppct'] = t['POPPCT10']
changes['2000']['areapct'] = t['AREALANDPCT10PT']
changes['2010'] = {}
changes['2010']['geoid'] = t['GEOID10']
changes['2010']['part'] = t['PART00']
changes['2010']['poppct'] = t['POPPCT00']
changes['2010']['areapct'] = t['ARELANDPCT00PT']
tract1 = t['GEOID00']
tract2 = str(t['GEOID10'])
try:
final['2000'][tract1].append(changes['2010'])
except:
final['2000'][tract1] = []
final['2000'][tract1].append(changes['2010'])
try:
final['2010'][tract2].append(changes['2000'])
except:
final['2010'][tract2] = []
final['2010'][tract2].append(changes['2000'])
for c in changed_sort:
if t['GEOID10'] == c['GEOID10']:
changedIDs.append(c['GEOID10'])
print "Total rows processed:", row_count
writeout = json.dumps(final['2000'], sort_keys=True, separators=(',',':'))
f_out = open('data/json/2000.json', 'wb')
f_out.writelines(writeout)
f_out.close()
writeout = json.dumps(final['2010'], sort_keys=True, separators=(',',':'))
f_out = open('data/json/2010.json', 'wb')
f_out.writelines(writeout)
f_out.close()
writeout = json.dumps(changedIDs, sort_keys=True, separators=(',',':'))
f_out = open('data/json/changed_2010.json', 'wb')
f_out.writelines(writeout)
f_out.close()
# Run function
parseData()