将python中的blf-files分为较小的文件

发布于 2025-01-22 19:56:21 字数 199 浏览 0 评论 0 原文

我想知道是否可以在Python中拆分BLF文件?我知道有一个支持BLF文件的库(CAN),但找不到有关如何拆分/保存的文档。我可以阅读BLF文件:

 import can
 log = can.BLFReader("logfile.blf")

如果有人知道如何将该文件分开并将其保存到较小的BLF文件中,请感谢任何帮助。

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:

 import can
 log = can.BLFReader("logfile.blf")

Would appreciate any help if anybody has knowledge in how I would split this file, and save it into smaller blf-files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所谓啦 2025-01-29 19:56:21

您可以读取logfile并将所有消息写入新文件,更改每个n消息(在我的示例n = 100中,非常低)。我发现看,否则我将不知道如何通过 log_in (在这种情况下为生成器)恢复迭代,或者如何轻松获得 object_count

import can

OUTPUT_MAX_SIZE = 100

with open("blf_file.blf", 'rb') as f_in:
    log_in = can.io.BLFReader(f_in)
    log_in_iter = log_in.__iter__()
    object_count = log_in.object_count
    i = 0
    while i*OUTPUT_MAX_SIZE < object_count:
        i += 1
        with open(f"smaller_file{i}.blf", 'wb') as f_out:
            log_out = can.io.BLFWriter(f_out)
            j = 0
            while j < OUTPUT_MAX_SIZE:
                log_out.on_message_received(log_in_iter.__next__())
                j += 1
            log_out.stop()

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:

import can

OUTPUT_MAX_SIZE = 100

with open("blf_file.blf", 'rb') as f_in:
    log_in = can.io.BLFReader(f_in)
    log_in_iter = log_in.__iter__()
    object_count = log_in.object_count
    i = 0
    while i*OUTPUT_MAX_SIZE < object_count:
        i += 1
        with open(f"smaller_file{i}.blf", 'wb') as f_out:
            log_out = can.io.BLFWriter(f_out)
            j = 0
            while j < OUTPUT_MAX_SIZE:
                log_out.on_message_received(log_in_iter.__next__())
                j += 1
            log_out.stop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文