简体   繁体   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?

I have a CSV file which contains 38 columns. 我有一个包含38列的CSV文件。 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). 我正在尝试使用Python的MySQLdb库将其插入到具有39列(第一个是CSV中没有的唯一num id)的MySQL表(已经存在,没有记录)中。 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. 值得注意的是,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()

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. 我已经更改了代码,但是现在我仍然在第一个“日期”列出现错误,该列出现在必须接受空白第一行的MySQL表中。 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 . Python中的索引从0开始。

So, if row has a len of 38 , you can index row[0] to row[37] included. 因此,如果rowlen38 ,则可以将row[0]索引为包含的row[37] Your attempt to index row[38] therefore causes the error you see. 因此,您尝试对row[38]进行索引会导致看到错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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