Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- import sys
- from os import listdir
- from os.path import isfile, join
- import csv
- import re
- import io
- try:
- con = db_connect("dbname = '[redacted]' user = '[redacted]' password = '[redacted]' host = '[redacted]'")
- except:
- print("Can't connect to database.")
- sys.exit(1)
- cur = con.cursor()
- upload_file = io.StringIO()
- file_list = [f for f in listdir(mypath) if isfile(join(mypath, f))]
- for file in file_list:
- id_match = re.search(r'.*-(d+).csv', file)
- if id_match:
- id = id_match.group(1)
- file_name = format(id_match.group())
- with open(mypath+file_name) as fh:
- id_reader = csv.reader(fh)
- next(id_reader, None) # Skip the header row
- for row in id_reader:
- [stuff goes here to get desired values from file]
- if upload_file.getvalue() != '': upload_file.write('n')
- upload_file.write('{0}t{1}t{2}'.format(id, [val1], [val2]))
- print(upload_file.getvalue()) # prints output that looks like I expect it to
- # with thousands of rows that seem to have the right values in the right fields
- cur.copy_from(upload_file, '[my_table]', sep='t', columns=('id', 'col_1', 'col_2'))
- con.commit()
- if con:
- con.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement