简体   繁体   中英

Python writing a long int as a binary value to a file

I have been trying to take a long int (m39 prime) and store it as a binary file. When I attempt to write the bin value to a file it is written as a string.

>>> m39 = bin(2**13466917-1)
>>> open('m39', 'wb').write(m39) 
madsc13ntist@jaberwock:~/Desktop$ xxd m39 | head
0000000: 3062 3131 3131 3131 3131 3131 3131 3131  0b11111111111111
0000010: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000020: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000030: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000040: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000050: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000060: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000070: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111
0000080: 3131 3131 3131 3131 3131 3131 3131 3131  1111111111111111

I'm feeling pretty certain that there is a ridiculously simple answer to this but I haven't been successful at converting m39 into a bytearray or a buffer for writing. Should I be using io or memoryview, etc.

Many thanks in advance for any assistance anyone can provide. :)

I am using Python 2.7.3 and I prefer to us builtin modules if at all possible.

EDIT: I am attempting to store the value as a binary file which would occupy much less space on the disk/memory. I understand that the bin type is a string in python but I am attempting to write \\x11\\x11\\x11\\x11 not \\x31\\x31\\x31\\x31 . My intent is not to print a string representation of the value, but to store it efficiently for later use/manipulation.

您应该使用结构模块或数组,具体取决于数据的结构(即,对于同质值数组,数组将更简单/更快)。

The Python pickle module is pretty efficient. The representation is only 8 bytes longer than the raw binary value can be stored into. This works in Python 2.X or 3.X:

import pickle
m39 = 2**13466917-1
with open('m39.dat','wb') as f:
    pickle.dump(m39,f,pickle.HIGHEST_PROTOCOL)

Resulting hex dump (1,683,373 bytes):

 80 03 8B A5 AF 19 00 FF FF FF ... FF FF FF 1F 2E

To read back:

import pickle
with open('m39.dat','rb') as f:
   m39 = pickle.load(f)

Python 3 also has to_bytes and from_bytes methods on integers, but requires a little more work as the length of the integer in bytes has to be computed.

import math
m39 = 2**13466917-1
s = m39.to_bytes(math.ceil(m39.bit_length()/8),'little')
with open('m39.dat','wb') as f:
    f.write(s)

Resulting hex dump (1,683,365 bytes):

 FF FF FF FF FF FF FF FF FF FF ... FF FF FF FF 1F

To read back:

with open('m39.dat','rb') as f:
    data = f.read()
m39 = int.from_bytes(data,'little')

Obviously there is a pattern there and it would be more efficient to just store the exponent of the prime instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM