Binary buffer in Python

后端 未结 3 1380
悲&欢浪女
悲&欢浪女 2020-12-02 15:27

In Python you can use StringIO for a file-like buffer for character data. Memory-mapped file basically does similar thing for binary data, but it requires a file that is use

3条回答
  •  醉话见心
    2020-12-02 15:55

    Look at the struct package: https://docs.python.org/library/struct.html, it allows you to interpret strings as packed binary data.

    Not sure if this will completely answer your question but you can use struct.unpack() to convert binary data to python objects.

    
    import struct
    f = open(filename, "rb")
    s = f.read(8)
    x, y = struct.unpack(">hl", s)
    
    

    int this example, the ">" tells to read big-endian the "h" reads a 2-byte short, and the "l" is for a 4-byte long. you can obviously change these to whatever you need to read out of the binary data...

提交回复
热议问题