Python 将csv导入MySQL数据库时,格式字符串参数不足
我需要通过Python 将csv导入MySQL数据库时,格式字符串参数不足,python,mysql-python,Python,Mysql Python,我需要通过行,行,行来纠正错误,但不能这样做。如何修复 编辑:打印行给出: #!/usr/bin/python import csv import MySQLdb mydb = MySQLdb.connect(host='localhost', user='english', passwd='english', db='english_mez') cursor = mydb.cursor() csv_data = csv.reader(file('final.csv')) for row
行,行,行
来纠正错误,但不能这样做。如何修复
编辑:打印行给出:
#!/usr/bin/python
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost', user='english', passwd='english', db='english_mez')
cursor = mydb.cursor()
csv_data = csv.reader(file('final.csv'))
for row in csv_data:
cursor.execute('INSERT INTO newsletter_subscriber(id, name, email )' 'VALUES("%s", "%s", "%s")', row[0], row[1], row[2])
#close the connection to the database.
mydb.commit()
cursor.close()
print "Done"
csv_数据中的行的:
数据=行[0]。拆分('\t')
如果len(数据)<4:继续
query=“”在新闻稿中插入订户(id、姓名、电子邮件)值
(%d,'%s','%s')”“”%(int(数据[0]),数据[1],数据[2])
cursor.execute(查询)
对于csv\u数据中的行:
数据=行[0]。拆分('\t')
如果len(数据)<4:继续
query=“”在新闻稿中插入订户(id、姓名、电子邮件)值
(%d,'%s','%s')”“”%(int(数据[0]),数据[1],数据[2])
cursor.execute(查询)
我可以知道final.csv的内容吗
请参阅更新的问题。我更新了答案。您需要data=row[0]。拆分('\t')
,因为它不是用逗号分隔的,而是用制表符分隔的。我可以知道final.csv
的内容,请参见更新的问题。我更新了答案。您需要data=row[0]。拆分('\t')
,因为它不是用逗号分隔的,而是用制表符分隔的。准备好了,列出索引超出范围错误。我正在用csv content.Nope,cursor.execute(“插入到新闻稿中”\u订户(id、姓名、电子邮件)值(?,,?),(数据[0],数据[1],数据[2])文件“/usr/lib/python2.7/dist packages/MySQLdb/cursors.py”,第159行,执行
中的query=query%db.literal(args)TypeError:在字符串格式化过程中并非所有参数都已转换“@user2032220 try(int(数据[0])、数据[1]、数据[2]))
@user2032220 i已更新答案。我不知道问题出在哪里抱歉:(尝试了导致query=“”在时事通讯中插入订阅者(id、姓名、电子邮件)值(%d、'%s'、'%s')””%(int(数据[0])、数据[1]、数据[2])索引器:列表索引超出范围
`已经尝试过,列表索引超出范围错误。我正在用csv内容更新问题。不,cursor.execute(“插入时事通讯”\u订户(id、姓名、电子邮件)值(?,,?),(数据[0],数据[1],数据[2])文件/usr/lib/python2.7/dist packages/MySQLdb/cursors.py”,第159行,
在execute`query=query%db中。literal(args)TypeError:在字符串格式化过程中并非所有参数都被转换,`@user2032220 try(int(数据[0])、数据[1]、数据[2])
@user2032220我更新了答案。我不知道问题出在哪里抱歉:(尝试了导致query=“”在时事通讯中插入订阅者(id、姓名、电子邮件)值(%d、'%s'、'%s')””%(int(数据[0])、数据[1]、数据[2])索引器:列表索引超出范围
`
#!/usr/bin/python
import csv
import MySQLdb
mydb = MySQLdb.connect(host='localhost', user='english', passwd='english', db='english_mez')
cursor = mydb.cursor()
csv_data = csv.reader(file('final.csv'))
for row in csv_data:
cursor.execute('INSERT INTO newsletter_subscriber(id, name, email )' 'VALUES("%s", "%s", "%s")', row[0], row[1], row[2])
#close the connection to the database.
mydb.commit()
cursor.close()
print "Done"
['6630\tCarmen Rocche\trocchecarmen@gmail.com\t\t\t\t\t']
['6631\tSuhasini\tkkalva14@hotmail.com\t\t\t\t\t']
['6632\tAmarjeet \tsweetylamba@gmail.com\t\t\t\t\t']
['6633\tFazali Hadi\tshewasb@yahoo.com\t\t\t\t\t']
['6634\tVishaka Samarakone\tshirashi.vishaka@gmail.com\t\t\t\t\t']
['6635\tLoemongga\tloemongga@yahoo.com\t\t\t\t\t']
for row in csv_data:
data=row[0].split('\t')
if len(data) < 4: continue
query="""insert into newsletter_subscriber (id, name, email) values
(%d, '%s', '%s')""" %(int(data[0]), data[1], data[2])
cursor.execute(query)