我构建了一个脚本,该脚本使用pandas从一个数据库中获取数据并将其迁移到另一个数据库(我必须对数据进行一些小的操作)-数据库架构和迁移完全相同。
运行df.to_sql
方法(为它提供目标数据库参数)时,出现以下错误:
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的值,如下所示:
当pandas读取json值时,它将“更改为”,这使得JSON无效,问题是为什么-您希望它能够从表中读取df,然后再写入相同的df无缝连接,我又该怎么做才能使这项工作呢?
编辑: 我试图添加以下代码行:
df['column_name'] = df['column_name'].replace("'", '"')
没有帮助