试图通过 python3 将 csv 文件导入 postgresql 数据库
trying to import csv file into postgresql database through python3
这是我的 cs50w 项目,我正在尝试将 books.csv 文件导入 postgresql 数据库,但出现了一些错误,我想我的脚本有问题,有人可以更正吗...
import psycopg2
import csv
#For connecting to the database
conn = psycopg2.connect("host=hostname_here port=5432 dbname=dbname_here user=username_here password=pass_here")
cur = conn.cursor()
#importing csv file
with open('books.csv', 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
cur.execute("INSERT INTO book VALUES (%s, %s, %s, %s)",
row
)
conn.commit()
Traceback (most recent call last):
File "import.py", line 15, in <module>
row
psycopg2.errors.SyntaxError: INSERT has more expressions than target columns
LINE 1: INSERT INTO book VALUES ('0380795272', 'Krondor: The Betraya...
csv 文件样本:
sample of csv file :
INSERT has more expressions than target columns.
您正试图在少于 4 列的 table 中插入包含 4 个值的行。
但是,如果 table 确实有 4 列,您需要检查您的数据源 (books.cvs。) 源数据可能有一些单引号或逗号。从文件中删除有问题的数据或修改您的程序以正确处理数据。
我的 postgres 中的问题已解决 table 我将 isbn 设置为整数,但我没有看到字母,现在我将 isbn 列更改为 varchar,问题已解决
这是我的 cs50w 项目,我正在尝试将 books.csv 文件导入 postgresql 数据库,但出现了一些错误,我想我的脚本有问题,有人可以更正吗...
import psycopg2
import csv
#For connecting to the database
conn = psycopg2.connect("host=hostname_here port=5432 dbname=dbname_here user=username_here password=pass_here")
cur = conn.cursor()
#importing csv file
with open('books.csv', 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
cur.execute("INSERT INTO book VALUES (%s, %s, %s, %s)",
row
)
conn.commit()
Traceback (most recent call last):
File "import.py", line 15, in <module>
row
psycopg2.errors.SyntaxError: INSERT has more expressions than target columns
LINE 1: INSERT INTO book VALUES ('0380795272', 'Krondor: The Betraya...
csv 文件样本: sample of csv file :
INSERT has more expressions than target columns.
您正试图在少于 4 列的 table 中插入包含 4 个值的行。
但是,如果 table 确实有 4 列,您需要检查您的数据源 (books.cvs。) 源数据可能有一些单引号或逗号。从文件中删除有问题的数据或修改您的程序以正确处理数据。
我的 postgres 中的问题已解决 table 我将 isbn 设置为整数,但我没有看到字母,现在我将 isbn 列更改为 varchar,问题已解决