我正在嘗試將資料從 CSV 遷移到 SQLite 資料庫。CSV 有 3 列,表只有 2 列。我試圖 pop() 出 row.split(',') 回傳的串列中的最后一個元素,但它給出了不同的錯誤
Github 鏈接到 csv
代碼:
seen = set();
with open("E:/Forage/Walmart/task4/shipping_data_1.csv",'r') as file:
for row in file:
if row in seen: continue
seen.add(row)
cur.execute("INSERT INTO product values(?,?)",row.split(',').pop())
con.commit();
con.close()
錯誤:什么時候
cur.execute("INSERT INTO product values(?,?)",row.split(',')); is used
-->Incorrect number of bindings supplied. The current statement uses 2, and there are 3 supplied.
和
cur.execute("INSERT INTO product values(?,?)",row.split(',').pop()); is used
-->Incorrect number of bindings supplied. The current statement uses 2, and there are 8
supplied.
uj5u.com熱心網友回復:
.pop() 不是您想要的(它回傳彈出的元素 - 第三列,然后 Python 試圖遍歷字串中的字母)。您需要改用切片。如果你想保留前兩列,做
row.split(',')[:2]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/536623.html
上一篇:如何比較不同時期的資料?
下一篇:如何訪問查詢塊之外的變數?
