Pandas to_sql方法在带有postgres引擎的json字段上失败

2024-06-26 07:39:27 发布

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

我构建了一个脚本,从一个数据库获取数据,并使用pandas将其迁移到另一个数据库(我必须对数据进行一些小的操作)-数据库模式和迁移完全相同

当运行df.to_sql方法(为其提供目标db参数)时,我得到以下错误:

  File "/Users/refael/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1413, in _handle_dbapi_exception
    exc_info
  File "/Users/refael/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 265, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Users/refael/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 248, in reraise
    raise value.with_traceback(tb)
  File "/Users/refael/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1170, in _execute_context
    context)
  File "/Users/refael/.local/share/virtualenvs/romee-dkO78zKu/lib/python3.6/site-packages/sqlalchemy/dialects/postgresql/psycopg2.py", line 683, in do_executemany
    cursor.executemany(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.DataError) invalid input syntax for type json
LINE 1: ...e, true, NULL, 5.0, true, false, NULL, 'DONE', 2, '{''COST_L...
                                                             ^
DETAIL:  Token "'" is invalid.
CONTEXT:  JSON data, line 1: {'...
 [SQL: 'INSERT INTO classifiers (id, name, type, score, score_type, last_run, ...)

我在这个表中只有一个JSON字段,所以我查看了要输入db的值,如下所示:

enter image description here

当pandas读取json值时,它似乎会将“to”改为“to”,这使得json无效,问题是为什么-您希望它能够从表中读取df,然后无缝地写入相同的df,以及我可以做些什么来实现这一点

编辑: 我尝试添加这行代码:

df['column_name'] = df['column_name'].replace("'", '"')

这没用


Tags: pysharedfsqlalchemylibpackageslocalline