在将csv导入到数据库之前,跳过第一行并删除某些字符

时间:2018-11-15 05:40:34

标签: python mysql csv

我是Python的新手,我的任务是将csv导入mysql数据库。我的csv文件中包含以下示例值:

SHA1,VSDT,TRX
005c41fc0f8580f51644493fcbaa0d2d468312c3,(WIN32 (EXE 7-2)),Ransom.Win32.TRX.XXPE50FFF027,
006ea7ce2768fa208ec7dfbf948bffda9da09e4e,WIN32 EXE 2-2,TROJ.Win32.TRX.XXPE50FFF027,

我的问题是,在导入数据库之前,如何仅在第二列的字符串的起点和终点删除"( "")"

我有这段代码可以导入csv

import csv
import mysql.connector
file = open(fullPath, 'rb')  
csv_data = csv.reader(file)

mycursor = mydb.cursor()
cursor = mydb.cursor()
for row in csv_data:
cursor.execute('INSERT INTO jeremy_table_test(sha1,vsdt,trendx)'
               'VALUES(%s, %s, %s)',[(row[0]),(row[1]),(row[2]))
mydb.commit()
cursor.close()
print("Done")

2 个答案:

答案 0 :(得分:2)

在读入时跳过行,而不是在写时跳过。

with open(fullPath, 'rb') as file:
    csv_data = csv.reader(file)
    next(csv_data)
    mycursor = mydb.cursor()
    cursor = mydb.cursor()
    for row in csv_data:
            cursor.execute('INSERT INTO jeremy_table_test(sha1,vsdt,trendx)'
                'VALUES(%s, %s, %s)',[(row[0]),(row[1]),(row[2]))
    mydb.commit()
    cursor.close()
    print("Done")

答案 1 :(得分:1)

MySQL LOAD DATA工具可能可以在这里完成您想要的操作。 LOAD DATA调用可能如下所示:

LOAD DATA INFILE 'path/to/rb'
INTO TABLE jeremy_table_test
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n' -- or '\n'
IGNORE 1 LINES
(sha1, @var1, trendx)
SET vsdt = TRIM(TRAILING ')' FROM TRIM(LEADING '(' FROM @var1));

要通过您的Python代码进行此调用,您可以尝试执行以下操作:

query = "LOAD DATA INFILE 'path/to/rb' INTO TABLE jeremy_table_test FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' IGNORE 1 LINES (sha1, @var1, trendx) SET vsdt = TRIM(TRAILING ')' FROM TRIM(LEADING '(' FROM @var1))"
cursor.execute(query)
connection.commit()