How to write individual bits to a text file in python?

后端 未结 3 449
清酒与你
清酒与你 2020-12-16 04:56

Suppose I have a number like 824 and I write it to a text file using python. In the text file, it will take 3 bytes space. However, If i represent it using bits, it has the

相关标签:
3条回答
  • 2020-12-16 05:04

    Have a look at struct:

    >>> struct.pack("h", 824)
    '8\x03'
    
    0 讨论(0)
  • 2020-12-16 05:08

    I think what you want is to open the file in binary mode:

    open("file.bla", "wb")
    

    However, this will write an integer to the file, which will probably be 4 bytes in size. I do not know if Python has a 2 byte integer type. But you can circumvent that by encoding 2 16 bit number in one 32 bit number:

    a = 824
    b = 1234
    c = (a << 16) + b
    
    0 讨论(0)
  • 2020-12-16 05:23

    The struct module is what you want. From your example, 824 = 0000001100111000 binary or 0338 hexadecimal. This is the two bytes 03H and 38H. struct.pack will convert 824 to a string of these two bytes, but you also have to decide little-endian (write the 38H first) or big-endian (write the 03H first).

    Example

    >>> import struct
    >>> struct.pack('>H',824) # big-endian
    '\x038'
    >>> struct.pack('<H',824) # little-endian
    '8\x03'
    >>> struct.pack('H',824)  # Use system default
    '8\x03'
    

    struct returns a two-byte string. the '\x##' notation means (a byte with hexadecimal value ##). the '8' is an ASCII '8' (value 38H). Python byte strings use ASCII for printable characters, and \x## notation for unprintable characters.

    Below is an example writing and reading binary data to a file. You should always specify the endian-ness when writing to and reading from a binary file, in case it is read on a system with a different endian default:

    import struct
    
    a = 824
    bin_data = struct.pack('<H',824)
    print 'bin_data length:',len(bin_data)
    
    with open('data.bin','wb') as f:
        f.write(bin_data)
    
    with open('data.bin','rb') as f:
       bin_data = f.read()
       print 'Value from file:',struct.unpack('<H',bin_data)[0]
    
    print 'bin_data representation:',repr(bin_data)
    for i,c in enumerate(bin_data):
        print 'Byte {0} as binary: {1:08b}'.format(i,ord(c))
    

    Output

    bin_data length: 2
    Value from file: 824
    bin_data representation: '8\x03'
    Byte 0 as binary: 00111000
    Byte 1 as binary: 00000011
    
    0 讨论(0)
提交回复
热议问题