-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloader.py
executable file
·75 lines (64 loc) · 2.14 KB
/
loader.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
75
#!/usr/bin/env python
import csv
import os
import pymysql
# get default env configs
conf = {
'db_host': 'localhost',
'db_port': 3306,
'db_name': 'tax_rates',
'db_user': 'user_tax',
'db_pass': 'unclesam'
}
def load_env():
try:
env_conf = {
'db_host': os.environ['TAX_RATES_DB_HOST'],
'db_port': int(os.environ['TAX_RATES_DB_PORT']),
'db_name': os.environ['TAX_RATES_DB_NAME'],
'db_user': os.environ['TAX_RATES_DB_USER'],
'db_pass': os.environ['TAX_RATES_DB_PASS']
}
conf.update(env_conf)
except KeyError:
print ("Missing environment variables so using default")
'''
Database fields
state, zip_code, tax_region_name, state_rate, est_combined_rate, est_county_rate, est_city_rate, est_special_rate, risk_level
'''
def load_files():
try:
# connect to database
conn = pymysql.connect(
conf['db_host'],
conf['db_user'],
conf['db_pass'],
conf['db_name'] )
# prepare a cursor object using cursor() method
cursor = conn.cursor()
# prepare query
insert_query = '''
INSERT INTO rate (state, zip_code, tax_region_name,
state_rate, est_combined_rate, est_county_rate,
est_city_rate, est_special_rate, risk_level)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
'''
# load files in dir and insert into database
for root, dirs, files in os.walk('./data/TAXRATES_ZIP5'):
for file in files:
with open('./data/TAXRATES_ZIP5/{}'.format(file), 'r') as f:
reader = csv.reader(f)
next(reader) # skip header row
for row in reader:
cursor.execute(insert_query, row)
# commit to db and close up
conn.commit()
conn.close()
except Exception as err:
print ('Error loading files into database: ', err)
def main():
print ('Loading ENV config...')
load_env()
print ('Loading CSV files...')
load_files()
if __name__ == '__main__': main()