[英]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.