簡體   English   中英

用於CSV的Python MySQLdb導入到MySQL表中的列比CSV /列IDX超出范圍?

[英]Python MySQLdb for CSV import into MySQL table with more columns than CSV / list idx out of range?

我有一個包含38列的CSV文件。 我正在嘗試使用Python的MySQLdb庫將其插入到具有39列(第一個是CSV中沒有的唯一num id)的MySQL表(已經存在,沒有記錄)中。 我下面的腳本給我一個錯誤...

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中的幾列都有空白的第一記錄(行),並且由於實際記錄從第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()

更新

我已經更改了代碼,但是現在我仍然在第一個“日期”列出現錯誤,該列出現在必須接受空白第一行的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()

Python中的索引從0開始。

因此,如果rowlen38 ,則可以將row[0]索引為包含的row[37] 因此,您嘗試對row[38]進行索引會導致看到錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM