bitarray.to01()不会在字符串中返回0和1(Python) [英] bitarray.to01() doesn't return only 0s and 1s in string (Python)
问题描述
我使用库 bitarray
来管理我的位转换并在Python中编写二进制文件。写入文件之前的bitarray.to01()长度 4807100171
。出于某种原因,我无法理解,从文件( b.fromfile(file)
)获取位后,然后转换为0和1的字符串,其中 to01()
,我的字符串中不仅有0和1( \x00
),然后当我使用它,我得到这个错误:
ValueError:无效文字为int()与基2:'0000000000000000\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\\ \\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00'
我想知道字符串是否可以有大小限制一个文件或一些问题。如果是这样,我还没有找到任何关于它的信息。
编辑:
重现问题:
$ b $ p
$ bb = bitarray(4807100171 )
b.setall(False)
如果re.match(^ [\d] + $,b.to01()):
printthere is there只有这个字符串中的数字。
else:
打印这个字符串中不仅有数字。
**编辑#2:
但是,如果我使用 platform.architecture()
和 sys.maxint
来检查我的机器,我可以得到: p>
在[1]中:import platform,sys
在[5]中:platform.architecture(),sys.maxint
Out [5]:(('64bit',''),9223372036854775807)
,这大约是2 ^ 63。它是如何在2 ^ 32截断的?
我有4GB的内存。我得到2 ^ 32 * 1.16415e-10 * 8(因为我将它转换为字符串)〜= 4GB ...但是,这是一个64位机器的事实呢?
在你的机器上不能有内存来运行这个大小的bitarray上的 to01
方法。该字符串将使用每个数字一个字节(至少) - 并且你有超过2 ** 32个数字。由于你不是swappign或者出现内存错误,所以你可能会在bitarray中遇到一些错误 -
但是......退后一步!
为什么在地球上你会喜欢一个40亿位的0和1字符串?打印你的自己一个Matrix主题赛车跑道??
如果你需要将甚至数十万个数字转换为0和1,寻找某种模式,你最好交互地做,每次转换几个字节,而不是你在那里尝试。
I use the library bitarray
to manage my bits conversion and to write a binary file in Python. The bitarray.to01() before writing to file is of length 4807100171
. For some reason I can't make sense of, after getting the bits fromfile (b.fromfile(file)
) and then converted to a string of 0s and 1s with to01()
, there is not only 0s and 1s in my string (\x00
) and then, when I work with it, I get this error:
ValueError: invalid literal for int() with base 2: '0000000000000000\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
I wonder if there could be a size limit for the string coming from a file or some issues as such. If so, I haven't found anything about it...
Edit:
Here's a way to reproduce the problem:
import re
from bitarray import bitarray
b = bitarray(4807100171)
b.setall(False)
if re.match("^[\d]+$", b.to01()):
print "there is only digits in this string."
else:
print "there is not only digits in this string."
** Edit #2:
However, if I check my machine using platform.architecture()
and sys.maxint
, I get this:
In [1]: import platform, sys
In [5]: platform.architecture(), sys.maxint
Out[5]: (('64bit', ''), 9223372036854775807)
So, this is approximately 2^63. How come it truncates at 2^32? I have 4GB of ram. I get that 2^32*1.16415e-10*8 (since I'm converting it to a string) ~= 4GB... But what about the fact that this is a 64bit machine?
ould not have memory on your machine to run the to01
method on a bitarray that size. The string will use one byte per digit (at least) - and you hae more than 2**32 digits. Since you are not swappign or getting out of memory errors, you may have hit some bug in bitarray --
But...step back!
Why on Earth woul you like a 4 billion digit string of "0" and "1"s? Print your self a Matrix themed racing track??
If you need to convert even a few hundred thousand digits to 0s and 1s , to look for some pattern, or whatever, you better doing it interactively, converting a few bytes at a time than wathever you are trying there.
这篇关于bitarray.to01()不会在字符串中返回0和1(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!