I’m wondering if it is possible to split BLF-files in Python? I know that there is a library (can) that supports BLF-files, but find no documentation on how to split/save. I can read a BLF-file with:
JavaScript
x
3
1
import can
2
log = can.BLFReader("logfile.blf")
3
Would appreciate any help if anybody has knowledge in how I would split this file, and save it into smaller blf-files.
Advertisement
Answer
You can read your logfile and write all messages to new files, changing file every N messages (in my example N=100, which is very low). I find it very useful to look at the doc, otherwise I wouldn’t know how to resume iteration over log_in
(in this case as a generator) or how to easily get object_count
:
JavaScript
1
19
19
1
import can
2
3
OUTPUT_MAX_SIZE = 100
4
5
with open("blf_file.blf", 'rb') as f_in:
6
log_in = can.io.BLFReader(f_in)
7
log_in_iter = log_in.__iter__()
8
object_count = log_in.object_count
9
i = 0
10
while i*OUTPUT_MAX_SIZE < object_count:
11
i += 1
12
with open(f"smaller_file{i}.blf", 'wb') as f_out:
13
log_out = can.io.BLFWriter(f_out)
14
j = 0
15
while j < OUTPUT_MAX_SIZE:
16
log_out.on_message_received(log_in_iter.__next__())
17
j += 1
18
log_out.stop()
19