基于python-can的blf格式日志转csv格式脚本
import sys
import binascii
import can
import time
from datetime import datetime
# 时间戳转换为真实时间
def timestamp_convert_realtime(ts):
return str(datetime.fromtimestamp(float(ts)))
def main():
"""The transcoder"""
with open("blf2csv.csv", mode="a+") as f:
f.write("timestamp" + 23 * " " + "id dlc Data\n")
# 读取文件里的内容,写入新的文件
with can.LogReader(sys.argv[1]) as reader:
for msg in reader:
row = ' '.join([
timestamp_convert_realtime(repr(msg.timestamp)),
hex(msg.arbitration_id),
str(msg.dlc),
(binascii.hexlify(msg.data, " ")).decode("utf-8")
])
f.write(row + "\n")
if __name__ == "__main__":
start_time = time.time()
main()
end_time = time.time()
print(f"用时{int(end_time - start_time)}s")
以上只是一种简单转换思路,通过命令行方式进行执行,仅供参考。