I have a CSV file which contains 38 columns. I am trying to use MySQLdb library of Python to insert it into a MySQL table (already existing, with no records) that has 39 columns (1st is a unique num id not in CSV). My script below is giving me a an error...
row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[
10], row[11], row[12], row[13], row[14], row[15], row[16], row[17], row[18], row
[19], row[20], row[21], row[22], row[23], row[24], row[25], row[26], row[27], ro
w[28], row[29], row[30], row[31], row[32], row[33], row[34], row[35], row[36], r
ow[37], row[38],)
IndexError: list index out of range
It is worth noting that several of the columns in the CSV have a 1st record(row) which is blank, and I am not importing the header(field names) row as the actual records begin on row 7. Any idea why I'm getting the error and no data.
import csv
import MySQLdb
csv.register_dialect('pipes', delimiter='|')
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='****',
db='artefacts')
cursor = mydb.cursor()
csv_data = csv.reader(file('$MT_pipe_.csv'), dialect = 'pipes')
for idx, row in enumerate(csv_data):
if idx > 5:
cursor.execute('INSERT INTO `nettop_master` VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,)',
row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11],
row[12], row[13], row[14], row[15], row[16], row[17], row[18], row[19], row[20], row[21],
row[22], row[23], row[24], row[25], row[26], row[27], row[28], row[29], row[30], row[31],
row[32], row[33], row[34], row[35], row[36], row[37], row[38],)
#close the connection to the database.
else:
continue
mydb.commit()
cursor.close()
UPDATE
I've changed my code but now I am still getting an error for the 1st "date" column to appear in the MySQL table which has to accept a blank first row. I think, for dates at least, the fact that the 1st row of the field is blank, is throwing an error like this...
_mysql_exceptions.OperationalError: (1292, "Incorrect date value: ' ' f
or column 'shortfilename_mdate' at row 1")
Here is my newest code...
import csv
import MySQLdb
csv.register_dialect('pipes', delimiter='|')
mydb = MySQLdb.connect(host='localhost',
user='root',
passwd='****',
db='artefacts')
cursor = mydb.cursor()
dir = "C:\\Users\**\\\Documents\\webapp_1010\\cyber\\"
csv_data = csv.reader(file('$MT_pipe_.csv'), dialect = 'pipes')
for idx, row in enumerate(csv_data):
if idx <= 6:
pass
else:
cursor.execute("INSERT INTO `artefacts`.`nettop_master`\
(`evidence_id`, `entry`, `sequence_nbr`, `parent`, `parent_sequence_nbr`, `SI_mdate`,
`SI_mtime`, `SI_adate`, `SI_atime`, `SI_cdate`, `SI_ctime`, `SI_bdate`, `SI_btime`, `FN_mdate`,
`FN_mtime`, `FN_adate`, `FN_atime`, `FN_cdate`, `FN_ctime`, `FN_bdate`, `FN_btime`, `typeof`,
`extension`, `size`, `name`, `path`, `symbolic_link`, `object_id`, `ads_metadata`,`time_warning`,
`shortfilename_mdate`, `shortfilename_mtime`, `shortfilename_adate`, `shortfilename_atime`,
`shortfilename_cdate`, `shortfilename_ctime`, `shortfilename_bdate`, `shortfilename_btime`,
`extracted_filepath`)\
VALUES (1, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s,
%s, %s, %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()
Indexing in Python starts from 0 .
So, if row
has a len
of 38
, you can index row[0]
to row[37]
included. Your attempt to index row[38]
therefore causes the error you see.
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.