Reading a binary file into a struct

前端 未结 4 635
面向向阳花
面向向阳花 2020-12-16 04:58

I have a binary file with a known format/structure.

How do I read all the binary data in to an array of the structure?

Something like (in pseudo code)

<
4条回答
  •  生来不讨喜
    2020-12-16 06:03

    Actually it looks like you're trying to read a list (or array) of structures from the file. The idiomatic way to do this in Python is use the struct module and call struct.unpack() in a loop—either a fixed number of times if you know the number of them in advance, or until end-of-file is reached—and store the results in a list. Here's an example of the latter:

    import struct
    
    struct_fmt = '=5if255s' # int[5], float, byte[255]
    struct_len = struct.calcsize(struct_fmt)
    struct_unpack = struct.Struct(struct_fmt).unpack_from
    
    results = []
    with open(filename, "rb") as f:
        while True:
            data = f.read(struct_len)
            if not data: break
            s = struct_unpack(data)
            results.append(s)
    

    The same results can be also obtained slightly more concisely using a list comprehension along with a short generator function helper (i.e. read_chunks() below):

    def read_chunks(f, length):
        while True:
            data = f.read(length)
            if not data: break
            yield data
    
    with open(filename, "rb") as f:
        results = [struct_unpack(chunk) for chunk in read_chunks(f, struct_len)]
    

    Update

    You don't, in fact, need to explicitly define a helper function as shown above because you can use Python's built-in iter() function to dynamically create the needed iterator object in the list comprehension itself like so:

    from functools import partial
    
    with open(filename, "rb") as f:
        results = [struct_unpack(chunk) for chunk in iter(partial(f.read, struct_len), b'')]
    

提交回复
热议问题