Python candas转换BLF至CSV效率低下,求最优替代方案
BLF转CSV速度优化方案
基于candas的核心优化方案
- 只加载目标信号:跳过无关信号的解析,直接过滤出需要的
AVGcellTemperature,大幅减少数据处理量
import candas as cd db = cd.load_dbc("dbc_folder") # 仅加载指定信号,避免处理冗余数据 log = cd.from_file("my_file.blf", signals=["AVGcellTemperature"]) # 直接转存为CSV log.to_csv("output.csv", index=False)
- 分块处理大文件:针对超大BLF文件,采用分块读取+逐块写入的方式,避免内存过载拖慢速度
import candas as cd db = cd.load_dbc("dbc_folder") # 开启分块模式,chunksize根据内存情况调整(示例为1万条/块) chunk_iterator = cd.from_file("my_file.blf", chunksize=10000) # 逐块写入CSV,第一块写入表头,后续块追加内容 is_first_chunk = True for chunk in chunk_iterator: chunk.to_csv("output.csv", mode="a", header=is_first_chunk, index=False) is_first_chunk = False
- 优化DBC加载:如果
dbc_folder包含多个DBC文件,只加载与AVGcellTemperature信号相关的文件,避免不必要的解析开销
高性能替代方案(换库提速)
如果candas的性能瓶颈无法突破,可以尝试更轻量高效的cantools库:
import cantools import pandas as pd from can import BLFReader # 加载目标DBC文件 db = cantools.database.load_file("target_dbc.dbc") # 获取包含目标信号的CAN消息ID target_msg_id = db.get_message_by_name("your_message_name").frame_id signals_records = [] # 流式读取BLF并解析目标信号 with BLFReader("my_file.blf") as reader: for msg in reader: if msg.arbitration_id == target_msg_id: decoded_data = db.decode_message(msg.arbitration_id, msg.data) signals_records.append({ "timestamp": msg.timestamp, "AVGcellTemperature": decoded_data["AVGcellTemperature"] }) # 转存为CSV pd.DataFrame(signals_records).to_csv("output.csv", index=False)
注:将
your_message_name替换为包含AVGcellTemperature信号的CAN消息名称
内容的提问来源于stack exchange,提问作者Hello Hai
相关产品推荐
相关产品推荐

