我有一個包含38列的CSV文件。我正在嘗試使用Python的MySQLdb庫將其插入到具有39列(第一個是唯一的數字標識不是CSV)的MySQL表(已存在,沒有記錄)中。 我下面的腳本給我一個錯誤...用於將CSV MySQL導入到MySQL表中的Python MySQLdb列數超過CSV /列表idx超出範圍?
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
值得注意的是,一些在CSV列的有1號記錄(行),這是空白,我不導入頭(字段名稱)行作爲實際記錄在第7行開始。任何想法爲什麼我得到的錯誤和沒有數據。
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
我已經改變了我的代碼,但現在我仍然得到一個錯誤的首屆「日期」一欄出現在其中必須接受一個空第一行的MySQL表。我想,對於日期至少,一個事實,即場的第一排爲空,則拋出這樣的錯誤...
_mysql_exceptions.OperationalError: (1292, "Incorrect date value: ' ' f
or column 'shortfilename_mdate' at row 1")
這是我最新的代碼...
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()
也許這是一個愚蠢的問題,但爲什麼不''解包'行? 'cursor.execute('INSERT INTO ...',* row [:38])'?這比用手寫出來要容易得多:-)。 – mgilson