我目前使用"copy_from“进行批量加载,但由于数据中存在双引号,这似乎会导致错误。
engineor = create_engine('oracle+cx_oracle://xxxx:xxxx@xxxxx:xxxx/?service_name=xxxxx')
sql = "select * from xxxxxx WHERE ROWNUM <= 10"
df = pd.read_sql(sql, engineor)
enginegp = create_engine('xxxxx@xxxxx:xxxx/xxxx')
connection = enginegp.raw_connection()
output = io.StringIO()
df.to_csv(output, sep='\t', header=False, index=False)
output.seek(0)
output.getvalue()
cur = connection.cursor()
cur.copy_from(output, 'test', null="")
connection.commit()
cur.close()我得到的错误是"DataError:缺少列的数据“,并带有某些列名。我尝试使用替换从数据中删除逗号,但仍然得到了不同列名的错误。
发布于 2018-06-13 01:40:14
使用CSV格式而不是伪造postgresql格式。与CSV不同,TSV不使用引号。TSV格式要求对非打印字符使用C转义--如果您的数据合并了任何新行,那么您将得到看起来很短的行。如果它包含任何选项卡,您将得到长行。
要执行CSV,您需要使用copy_expert,它将授予postgresql命令支持的所有选项的访问权限,包括CSV支持。
df.to_csv(output, header=False, index=False)..。
cur.copy_expert("COPY test FROM STDIN WITH CSV NULL '' ", output)https://stackoverflow.com/questions/50827777
复制相似问题