写一个小功能。
import cStringIO
import pandas as pd
from sqlalchemy import create_engine
path = "D://Users//xxxx/Desktop//"
file = pd.read_excel(path+'test.xlsx')
file['ismodified'] = 0
engine = create_engine('postgresql+psycopg2://xxx:xxx@ip:port/xxxx')
output = cStringIO.StringIO()
# ignore the index
file.to_csv(output, sep='\t', index=False, header=False)
output.getvalue()
# jump to start of stream
output.seek(0)
connection = engine.raw_connection()
cursor = connection.cursor()
# null value become ''
cursor.copy_from(output, 'table_name', null='')
connection.commit()
cursor.close()
excel格式如下:
数据库中格式如下: