Python 3.x psycopg2.DataError:双精度类型的输入语法无效:“0”;空";

Python 3.x psycopg2.DataError:双精度类型的输入语法无效:“0”;空";,python-3.x,postgresql,csv,psycopg2,Python 3.x,Postgresql,Csv,Psycopg2,我正试图使用copy-from命令和Python-psycopg2库上传一个CSV文件。我知道错误源于试图将char值插入双精度列,但在创建csv文件时,我将null值指定为“null”,并且在Postgres copy命令中,我还将null指定为“null”,因此我不确定上传时为什么不转换这些值 创建csv文件: with file_path.open(mode='w', newline='') as outfile: csv_writer = csv.writer(outfile,

我正试图使用copy-from命令和Python-psycopg2库上传一个CSV文件。我知道错误源于试图将char值插入双精度列,但在创建csv文件时,我将null值指定为
“null”
,并且在Postgres copy命令中,我还将null指定为
“null”
,因此我不确定上传时为什么不转换这些值

创建csv文件:

with file_path.open(mode='w', newline='') as outfile:
    csv_writer = csv.writer(outfile, delimiter=delimiter, quoting=csv.QUOTE_NONNUMERIC)

    if include_headers:
        csv_writer.writerow(col[0] for col in self.cursor.description)
    for row in self.cursor:
        csv_writer.writerow(['NULL' if value is None else value for value in row])
sql = """COPY {table} FROM STDIN WITH (DELIMITER '{sep}', NULL 'NULL', FORMAT CSV, HEADER True);""".format(table=table, sep=sep)

with open(file) as f:
    self.cursor.copy_expert(sql=sql, file=f, size=size)
self.commit()
psycopg2.DataError: invalid input syntax for type double precision:
"NULL" CONTEXT:  COPY test_table, line 25, column Lat:
"NULL"
将文件上载到postgres:

with file_path.open(mode='w', newline='') as outfile:
    csv_writer = csv.writer(outfile, delimiter=delimiter, quoting=csv.QUOTE_NONNUMERIC)

    if include_headers:
        csv_writer.writerow(col[0] for col in self.cursor.description)
    for row in self.cursor:
        csv_writer.writerow(['NULL' if value is None else value for value in row])
sql = """COPY {table} FROM STDIN WITH (DELIMITER '{sep}', NULL 'NULL', FORMAT CSV, HEADER True);""".format(table=table, sep=sep)

with open(file) as f:
    self.cursor.copy_expert(sql=sql, file=f, size=size)
self.commit()
psycopg2.DataError: invalid input syntax for type double precision:
"NULL" CONTEXT:  COPY test_table, line 25, column Lat:
"NULL"
错误:

with file_path.open(mode='w', newline='') as outfile:
    csv_writer = csv.writer(outfile, delimiter=delimiter, quoting=csv.QUOTE_NONNUMERIC)

    if include_headers:
        csv_writer.writerow(col[0] for col in self.cursor.description)
    for row in self.cursor:
        csv_writer.writerow(['NULL' if value is None else value for value in row])
sql = """COPY {table} FROM STDIN WITH (DELIMITER '{sep}', NULL 'NULL', FORMAT CSV, HEADER True);""".format(table=table, sep=sep)

with open(file) as f:
    self.cursor.copy_expert(sql=sql, file=f, size=size)
self.commit()
psycopg2.DataError: invalid input syntax for type double precision:
"NULL" CONTEXT:  COPY test_table, line 25, column Lat:
"NULL"

您应该使用
QUOTE\u MINIMAL
,否则您的
None
将在文件中显示为
“NULL”
(包括引号),这就是
COPY
无法正确识别它们的原因


但我认为您不必经历所有这些,因为
COPY
能够很好地处理CSV文件。打开文件并使用
copy_from
copy_to
方法。

您知道从sql查询创建csv文件的更好方法吗?必须迭代所有的值来转换空值,这样才能使用copy命令,这似乎效率低下。