Manipulating binary data in Python

前端 未结 7 1332
旧时难觅i
旧时难觅i 2021-02-07 07:16

I am opening up a binary file like so:

file = open(\"test/test.x\", \'rb\')

and reading in lines to a list. Each line looks a little like:

7条回答
  •  长情又很酷
    2021-02-07 07:51

    If you are willing to use NumPy and bitstream, you can do

    >>> from numpy import *
    >>> from bitstream import BitStream
    >>> raw = '\xbe\x00\xc8d\xf8d\x08\xe4.\x07~\x03\x9e\x07\xbe\x03\xde\x07\xfe\n'
    >>> stream = BitStream(raw)
    >>> stream.read(raw, uint8, len(stream) // 8)
    array([190,   0, 200, 100, 248, 100,   8, 228,  46,   7, 126,   3, 158,
             7, 190,   3, 222,   7, 254,  10], dtype=uint8)
    

提交回复
热议问题