开发者

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

开发者 https://www.devze.com 2023-02-14 20:31 出处:网络
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 following representation 00

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 following representation 0000001100111000 which is 2 bytes (16 bits). I was wondering how can I write bits to file in python, not bytes. If I can do that, the size of the file will be 2 bytes, not 3. Please provide code. I am using python 2.6. Also, I do not want to use any external modules that do 开发者_JAVA百科not come with the basic installation I tried below and gave me 12 bytes!

a =824;
c=bin(a)
handle = open('try1.txt','wb')
handle.write(c)
handle.close()


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


Have a look at struct:

>>> struct.pack("h", 824)
'8\x03'


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

精彩评论

暂无评论...
验证码 换一张
取 消