Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“如何修复”;json类型的输入语法无效;在使用copy方法将json str插入postgresql时,使用python_Python_Python 3.x_Postgresql_Psycopg2 - Fatal编程技术网

“如何修复”;json类型的输入语法无效;在使用copy方法将json str插入postgresql时,使用python

“如何修复”;json类型的输入语法无效;在使用copy方法将json str插入postgresql时,使用python,python,python-3.x,postgresql,psycopg2,Python,Python 3.x,Postgresql,Psycopg2,我试图使用copy方法将json类型的数据插入到postgresql数据库中。然而,我得到下面提到的错误 invalid input syntax for type json DETAIL: Expected end of input, but found ""aco"". CONTEXT: JSON data, line 1: "{""aco"... COPY flights2016jsn04, line 1, column flight: ""{""aco"": [""AXE"", "

我试图使用copy方法将json类型的数据插入到postgresql数据库中。然而,我得到下面提到的错误

invalid input syntax for type json

DETAIL:  Expected end of input, but found ""aco"".
CONTEXT:  JSON data, line 1: "{""aco"...
COPY flights2016jsn04, line 1, column flight: ""{""aco"": [""AXE"", ""AXE"", ""AXE"", ""AXE""], ""dist2Org"": 984753, ""flight_att"": {""ypos"": [8..." 
我不确定这个问题是什么,我在网上读到的内容有点混乱。下面是我的代码和一些示例数据

import json
import io
import pandas as pd
import psycopg2


dict_ = {"dist2Org": 984753, "aco": ["AXE", "AXE", "AXE", "AXE"],
     "flight_att": {"xpos": [823.08988, 6540.32231, 999, 33321],
                    "ypos": [823.08988, 6540.32231, 999, 33321], "zpos": [823.08988, 6540.32231, 999.33321]}}

json_ = json.dumps(dict_)

col_json = ["id", "flight"]
df = pd.DataFrame([65654, json_]).T

df.to_csv('test_df')
output = io.StringIO()
# ignore the index
df.to_csv(output, sep='\t', header=False, index=False)
output.getvalue()
# jump to start of stream
output.seek(0)

conn = None
try:
    # connection string
    conn_string = '{0}{1} {2}'.format("host='localhost' dbname=", 'postgres', "user='postgres' password='xxxx'")
    # conn_string = "host='localhost' dbname='postgres' user='postgres' password='xxxx'"

    # connect to the PostgreSQL database
    conn = psycopg2.connect(conn_string)

    # create a new cursor
    cur = conn.cursor()

    # load data

    cur.copy_from(output, 'flights2016jsn04', null="", columns=(col_json))

    # # commit the changes to the database
    conn.commit()

    # close communication with the database
    cur.close()

except (Exception, psycopg2.DatabaseError) as error:
    print(error)

finally:
    if conn is not None:
        conn.close()
\t
分隔文本格式不需要引号。 另一方面,Pandas使用
作为默认的引号字符,这在JSON中经常出现,需要转义

有两种选择:

  • 如果您知道数据中没有
    \t
    s,您可以尝试
    df.to_csv(sep='\t',quoting=csv.QUOTE_NONE)
  • 改为使用psycopg2复制csv

试试这个:cur.copy_from(输出'flights2016 jsn04',null='',columns=(col_json)).json不喜欢““但是”“@Martin,不幸的是,这也不起作用。我知道这个错误,当我的JSON有额外的引号或双引号时,它总是发生。”。我无法复制代码,因此我将尝试拍摄我的第二个想法:尝试在不转储df=pd.DataFrame([6565654,dict_]).TYes的情况下放置json!df.to_csv(输出,sep='\t',quoting=csv.QUOTE_NONE)有效。谢谢你的解释!