简体   繁体   中英

Python import CSV file data into a PostgreSQL

We are going to enter CSV file data using Python psycopg2 module. What should I do if my code changes data from cells in a CSV file to None?

import psycopg2
from datetime import datetime

datetime.now().month, datetime.now().day, datetime.now().hour, datetime.now().minute,datetime.now().second))
conn_string= "host='' dbname='' user='' password='' "
conn=psycopg2.connect(conn_string)
curs = conn.cursor()
f = open(r'output.csv', 'r')
curs.copy_from(f,'temp',sep=',',null='None' )
f.close()
conn.close()
datetime.now().month, datetime.now().day, datetime.now().hour, datetime.now().minute,datetime.now().second))
print("CSV file import done.")
import psycopg2
import csv
import io
from datetime import datetime

print ("시작 시간 %s년 %s월 %s일 %s시 %s분 %s초 " %(datetime.now().year, datetime.now().month, datetime.now().day, datetime.now().hour, datetime.now().minute,datetime.now().second))
conn_string = "host='' dbname='' user='' password='' "
conn = psycopg2.connect(conn_string)
cur = conn.cursor()

# Use the COPY function on the SQL we created above.
SQL_for_file_output = "COPY ({0}) TO STDOUT WITH CSV ".format("select * from sk_ds_service_age_p_202001")
# Set up a variable to store our file path and name.
t_path_n_file = "output.csv"
with open(t_path_n_file, 'w') as f_output:
    cur.copy_expert(SQL_for_file_output, f_output)
print ("종료 시간 %s년 %s월 %s일 %s시 %s분 %s초 " %(datetime.now().year, datetime.now().month, datetime.now().day, datetime.now().hour, datetime.now().minute,datetime.now().second))
print("CSV file export done.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM