Created
January 11, 2018 04:06
-
-
Save kshwetabh/df21b180aafc224b8748a59e983fe578 to your computer and use it in GitHub Desktop.
Revisions
-
kshwetabh created this gist
Jan 11, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ import psycopg2 conn = psycopg2.connect("host='localhost' port='5432' dbname='stocks' user='xxxxxx' password='xxxxxx'") cur = conn.cursor() SQL_STATEMENT = """ COPY %s FROM STDIN WITH CSV HEADER DELIMITER AS ',' """ def process_file(conn, table_name, file_object): cursor = conn.cursor() cursor.copy_expert(sql=SQL_STATEMENT % table_name, file=file_object) conn.commit() cursor.close() f = open(r'C:\Users\xxx\Desktop\AllStocksExtracted\code\result.csv', 'r') try: process_file(conn, 'stocks', f) finally: conn.close()