使用Python对特殊字符进行大容量加载

2024-10-03 21:31:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在使用“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:missing data for column”。我试着用replace从数据中删除逗号,但仍然得到一个不同列名的错误。在


Tags: 数据fromdfoutputsqlcreateconnectionengine
1条回答
网友
1楼 · 发布于 2024-10-03 21:31:02

使用CSV格式,而不是试图伪造postgresqltsv格式。 与CSV不同,TSV不使用引号。TSV格式要求对非打印字符使用C转义符-如果您的数据包含任何换行符,您将得到看起来很短的行。如果它包含任何制表符,您将得到长行。在

要实现CSV,您需要使用copy_expert,它将授予对postgresql copy命令支持的所有选项的访问权限,包括CSV支持。在

df.to_csv(output, header=False, index=False)

。。。在

^{pr2}$

相关问题 更多 >