How to pack an arbitrary sequence of bits in Python?

I want to encode / compress some binary image data as a sequence, if bit. (This sequence, generally speaking, has a length that does not fit neatly in a number of standard integer types.)

How can I do this without losing space? (I understand that if a sequence of bits does not have a "nice" length, there should always be a small amount [<1 byte] of remaining space at the very end.)

FWIW, I believe that no more than 3 bits will be required for each character that I want to encode. Does Python have built-in tools for this kind of work?

+3
source share
3 answers

, , bitstring bitarray, .

from bitstring import BitArray
s = BitArray('0b11011')
s += '0b100'
s += 'uint:5=9'
s += [0, 1, 1, 0, 1]
...
s.tobytes()

(, 0- > 7),

>>> symbols = [0, 4, 5, 3, 1, 1, 7, 6, 5, 2, 6, 2]
>>> BitArray().join(BitArray(uint=x, length=3) for x in symbols)
BitArray('0x12b27eab2')
>>> _.tobytes()
'\x12\xb2~\xab '

:

+4

bz2? , bz2.BZ2Compressor, chunked, bz2.compress . , , , , , .

, .

+2

Since you have character mapping with a 3-bit string, bitarray does a good job of encoding and decoding lists of characters in and out of bit arrays:

from bitarray import bitarray
from random import choice

symbols = {
    '0' : bitarray('000'),
    'a' : bitarray('001'),
    'b' : bitarray('010'),
    'c' : bitarray('011'),
    'd' : bitarray('100'),
    'e' : bitarray('101'),
    'f' : bitarray('110'),
    'g' : bitarray('111'),
}

seedstring = ''.join(choice(symbols.keys()) for _ in range(40))

# construct bitarray using symbol->bitarray mapping
ba = bitarray()
ba.encode(symbols, seedstring)

print seedstring
print ba

# what does bitarray look like internally?
ba_string = ba.tostring()
print repr(ba_string)
print len(ba_string)

Print

egb0dbebccde0gfdfbc0d0ccfcg0acgg0ccfga00
bitarray('10111101000010001010101001101110010100... etc.
'\xbd\x08\xaanQ\xf4\xc9\x88\x1b\xcf\x82\xff\r\xee@'
15

You can see that this list of 40 characters (120 bits) is encoded in 15-byte bitarray.

+2
source

Source: https://habr.com/ru/post/1793595/


All Articles