-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_setup.py
42 lines (31 loc) · 914 Bytes
/
db_setup.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
from models import *
from csv import DictReader
from peewee import SelectQuery
db.create_tables([
Voter
, Voter_Geocode
, Voter_History
], True)
voter_count = SelectQuery(Voter).count()
if voter_count < 121795:
print ' Importing voter records...'
with open('VRTAPE01KBIA_W_HEADERS.CSV') as in_file:
reader = DictReader(in_file)
for row in reader:
if row['vr_dob'] == '0':
row['vr_dob'] = None
else:
row['birth_year'] = row['vr_dob'][:4]
if len(row['vr_dob']) < 8:
row['vr_dob'] = None
elif row['vr_dob'][4:6] == '00' or row['vr_dob'][6:] == '00':
row['vr_dob'] = None
elif row['vr_dob'][:4] == '9999':
row['vr_dob'] = None
else:
row['vr_dob'] = row['vr_dob'][:4] + '-' + row['vr_dob'][4:6] + '-' + row['vr_dob'][6:]
for k, v in row.iteritems():
if v == '':
row[k] = None
with db.atomic():
Voter.create(**row)