How can I store and retrieve the number 2**10000 in a binary file in python without converting it to a string? Can it be stored as 10,000 bits?
+1
A:
It's not clear to me if you are asking in general how to store large integers in a binary file or if the number 2**10000 is significant. If it is significant then using over a kilobyte to store it is obviously very wasteful (I can write it in 8 characters!)
I'll assume the general case, but for starters you'd need 10001 bits to store 2**10000, not 10000, so there's a question over what to do about the extra 7 bits needed to pad to a byte boundary in the file. I'm just going to store it in 10008 bits (1251 bytes). This solution uses the bitstring module.
from bitstring import Bits
fout = open('bignumber', 'wb')
a = Bits(uint=2**10000, length=10008)
a.tofile(fout)
and to read it back:
the_number = Bits(filename='bignumber').uint
This really does just store the number and nothing else in the file.
Scott Griffiths
2010-04-15 07:06:06
In my case I do store exactly the same number of bits every time, so next time I look at the code I'll check if this is faster thanks.
Mark
2010-04-25 20:15:06