简体   繁体   中英

Error when importing csv into MySQL With Python

Hi I have only just started with Python,

I have written this .py file with a simple test and has worked fine but when I just simply added more %s's it bomb's out with:

Error: 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 Script:


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 File Format:

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

Any Ideas??

Thanks.

Check Check and Check Again!!

I had diffident no.s of fields..

Ie I if you get the above error it is normally because your csv has different amount to the %s place holders. you will get a column count error when there is a difference in you mysql table columns.

Thank You Adam Mihalcin for helping point this out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM