[英]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 文件樣本: csv 文件樣本:
INSERT 具有比目標列更多的表達式。
您正在嘗試在少於 4 列的表中插入具有 4 個值的行。
但是,如果該表確實有 4 列,則需要查看您的數據源(books.cvs)。源數據可能包含一些單引號或逗號。 從文件中刪除有問題的數據或修改您的程序以正確處理數據。
在我的 postgres 表中解決的問題我將 isbn 設置為 integer 但我沒有看到字母現在我將 isbn 列更改為 varchar 並且問題解決了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.