How do I convert .blf data from CAN to .csv using python

JulianWgs picture JulianWgs · Mar 26, 2018 · Viewed 10.6k times · Source

I have CAN-Data in the blf-format from the Vector software. For further investigation I want to convert it into csv format using python.

My progress so far:

import can
filename = "test.blf"
log = can.BLFReader(filename)

I dont know if thats the right way. I can't save "log" to a csv file now.

This might help

Answer

JulianWgs picture JulianWgs · Mar 27, 2018

List of that object does the trick

import can
import csv

filename = "test.blf"
log = can.BLFReader("test.blf")
log = list(log)

log_output = []

for msg in log:
    msg = str(msg)
    log_output.append([msg[18:26],msg[38:40],msg[40:42],msg[46],msg[62],msg[67:90]])

with open("output.csv", "w", newline='') as f:
    writer = csv.writer(f,delimiter=';', quotechar='\"', quoting=csv.QUOTE_ALL)
    writer.writerows(log_output)