[英]Error when importing csv into MySQL With Python
嗨,我才剛開始使用Python,
我已經用一個簡單的測試編寫了這個.py文件,並且運行良好,但是當我只是簡單地添加更多%s時,它的炸彈就出來了:
錯誤: File "/usr/lib/pymodules/python2.7/MySQLdb/cursors.py", line 159, in execute query = query % db.literal(args) TypeError: not enough arguments for format string
Python腳本:
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='******',
db='kestrel_keep')
cursor = mydb.cursor()
csv_data = csv.reader(file('data_csv.log'))
for row in csv_data:
cursor.execute('INSERT INTO `heating` VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,)',
row)
#close the connection to the database.
mydb.commit()
cursor.close()
import os
print "Done"
CSV檔案格式:
2013-02-21,21:42:00,-1.0,45.8,27.6,17.3,14.1,22.3,21.1,1,1,2,2
2013-02-21,21:48:00,-1.0,45.8,27.5,17.3,13.9,22.3,20.9,1,1,2,2
有任何想法嗎??
謝謝。
檢查檢查並再次檢查!
我的字段編號不同。
即,如果您收到上述錯誤,那通常是因為您的csv與%s占位符的金額不同。 當您的mysql表列存在差異時,您將收到列計數錯誤。
謝謝亞當·米哈爾辛(Adam Mihalcin)幫助指出這一點!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.