forked from neighdough/census
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdec_to_postgres.py
More file actions
executable file
·35 lines (31 loc) · 1.04 KB
/
dec_to_postgres.py
File metadata and controls
executable file
·35 lines (31 loc) · 1.04 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
'''
Created on Oct 25, 2013
@author: nfergusn
'''
import psycopg2 as pg
import os
sys.path.append('$HOME/source')
from caeser import utils
params = utils.connection_info('localhost', db='db')
engine = utils.connect(**params)
directory = r'E:\Data\Census\DEC\2010\sf1'
db = pg.connect(**params)
cursor = db.cursor()
for docRoot, dirs, files in os.walk(directory):
for f in files:
with open(os.path.join(docRoot, f), 'r') as f_in:
if f[2] == 'g':
dict = {}
for line in f_in.readlines():
dict[line]=(line[0:7],line[6:19], line[18:68],line[67:154],
line[153:165], line[164:172], line[171:201],
line[200:21])
tbl = 'geoheader'
else:
tbl = 'sf1_' + f[2:7]
cursor.copy_from(f_in, tbl, sep=',', null='')
db.commit()
print 'Successfully added ', f, ' to ', tbl, '\n'
cursor.close()
db.close()
print 'Process complete'