快速解析大型CSV文件

时间:2018-06-30 01:21:43

标签: python python-3.x csv

过去一周来,我一直受python的困扰,终于可以工作了,但是可以使用一些帮助来加快它的速度

该功能从汽车CAN总线上注销.CSV日志,并将其简化为与一组消息ID和遇到的消息ID匹配的记录列表。

文件为500,000行至50,000,000行。目前,我的笔记本电脑每行大约需要3.2uS。

CSV文件行如下所示:

Time [s],Packet,Type,Identifier,Control,Data,CRC,ACK
0.210436250000000,0,DATA,0x0CFAE621,0x8,0x02 0x50 0x00 0x00 0x04 0x01 0x00 0x29,0x19A8,NAK
...
...
52.936353750000002,15810,DATA,0x18FC07F4,0x8,0xF0 0x09 0x00 0x00 0xCE 0x03 0x92 0x20,0x0C47,ACK

所以第4个条目“ 0x0CFAE621”是消息ID,第6个条目“ 0xF0 0x09 0x00 0x00 0xCE 0x03 0x92 0x20”是数据

这用0x00FFFF00屏蔽,如果匹配则另存为[0xFAE600,'F0','09','00','00','CE','03','92','20'],尽管理想情况下,我想在此时将所有数据都转换为int,用int()包裹每个数据似乎非常缓慢(当时我想我可以通过命令进行十六进制-Int转换来改善它,但我没有确定如何做到)

len()和tree是由于消息数据可以为8条记录而为空,我再次觉得可能有更好的方法来完成此操作。

from tkinter import filedialog
from tkinter import Tk
import timeit

Tk().withdraw()
filename = filedialog.askopenfile(title="Select .csv log file", filetypes=(("CSV files", "*.csv"), ("all files", "*.*")))

if not filename:
    print("No File Selected")
else:
    CanIdentifiers = set()
    CanRecordData = []
    IdentifierList = {0x00F00100,0x00F00400,0x00FC0800,0x00FE4000,0x00FE4E00,0x00FE5A00,0x00FE6E00,0x00FEC100,0x00FEC300,0x00FECA00,0x00FEF100}
    mask = 0x00FFFF00
    loopcount = 0
    error = 0
    csvtype = 0

    start_time = timeit.default_timer()

    for line in filename.readlines():
        message = line.split(',')

        if csvtype == 1:
            if message[2] == "DATA":
                messageidentifier = int(message[3], 16) & mask
                if messageidentifier not in CanIdentifiers:
                    CanIdentifiers.add(messageidentifier)
                if messageidentifier in IdentifierList:
                    messagedata = message[5].split("0x")
                    size1 = len(messagedata)
                    if size1 == 2:
                        CanRecordData.append((messageidentifier, messagedata[1]))
                    if size1 == 3:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2]))
                    if size1 == 4:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3]))
                    if size1 == 5:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3], messagedata[4]))
                    if size1 == 6:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3], messagedata[4], messagedata[5]))
                    if size1 == 7:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3], messagedata[4], messagedata[5], messagedata[6]))
                    if size1 == 8:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3], messagedata[4], messagedata[5], messagedata[6], messagedata[7]))
                    if size1 == 9:
                        CanRecordData.append((messageidentifier, messagedata[1], messagedata[2], messagedata[3], messagedata[4], messagedata[5], messagedata[6], messagedata[7], messagedata[8]))

        if csvtype == 0:
            if message[0] == "Time [s]":
                csvtype = 1
            error += 1
            if error == 50:
                break
        loopcount += 1

    readtime = (timeit.default_timer() - start_time) * 1000000
    print(loopcount, "Records Processed at", readtime/loopcount, "uS per Record")

1 个答案:

答案 0 :(得分:3)

Pandas的read_csv()将为您提供一个数据框:

    Time [s]  Packet  Type  Identifier Control                                     Data     CRC  ACK
0   0.210436       0  DATA  0x0CFAE621     0x8  0x02 0x50 0x00 0x00 0x04 0x01 0x00 0x29  0x19A8  NAK
1  52.936354   15810  DATA  0x18FC07F4     0x8  0xF0 0x09 0x00 0x00 0xCE 0x03 0x92 0x20  0x0C47  ACK

然后,根据需要拆分数据字节:

import pandas as pd
df = pd.read_csv('t.csv')
df.Data.str.split(expand=True)

哪个给你:

      0     1     2     3     4     5     6     7
0  0x02  0x50  0x00  0x00  0x04  0x01  0x00  0x29
1  0xF0  0x09  0x00  0x00  0xCE  0x03  0x92  0x20

这将比Python循环快得多,并且存储也将更加紧凑-特别是如果您将十六进制数字解析为实际整数:convert pandas dataframe column from hex string to int