简体   繁体   English

bitarray.to01()不会仅在字符串中返回0和1(Python)

[英]bitarray.to01() doesn't return only 0s and 1s in string (Python)

I use the library bitarray to manage my bits conversion and to write a binary file in Python. 我使用库bitarray来管理位转换并在Python中编写二进制文件。 The bitarray.to01() before writing to file is of length 4807100171 . 写入文件之前,bitarray.to01()的长度为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: 由于某些原因,我无法理解,在从b.fromfile(file) fromfile( b.fromfile(file) )获得位,然后使用to01()将其转换为0和1的字符串后,我的字符串中不仅只有0和1 \\x00 ),然后当我使用它时,出现此错误:

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: **编辑#2:

However, if I check my machine using platform.architecture() and sys.maxint , I get this: 但是,如果我使用platform.architecture()sys.maxint检查我的机器, sys.maxint得到以下信息:

In [1]: import platform, sys
In [5]: platform.architecture(), sys.maxint
Out[5]: (('64bit', ''), 9223372036854775807)

So, this is approximately 2^63. 因此,这大约是2 ^ 63。 How come it truncates at 2^32? 它如何在2 ^ 32处截断? I have 4GB of ram. 我有4GB的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? 我得到了2 ^ 32 * 1.16415e-10 * 8(因为我将其转换为字符串)〜= 4GB ...但是,这是一台64位计算机又如何呢?

ould not have memory on your machine to run the to01 method on a bitarray that size. 您的计算机上没有内存,无法在该大小的to01上运行to01方法。 The string will use one byte per digit (at least) - and you hae more than 2**32 digits. 该字符串每位至少使用一个字节-且您拥有超过2 ** 32位数字。 Since you are not swappign or getting out of memory errors, you may have hit some bug in bitarray -- But...step back! 由于您不是swappign或没有出现内存不足错误,因此您可能遇到了位数组中的一些错误-但是...退后一步!

Why on Earth woul you like a 4 billion digit string of "0" and "1"s? 为什么在地球上您想要一个40亿位的字符串“ 0”和“ 1”? Print your self a Matrix themed racing track?? 打印自己的Matrix主题赛车场??

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. 如果您甚至需要将数十万个数字转换为0和1s,以查找某种模式或其他内容,则最好以交互方式进行操作,一次转换几个字节,而不用花很多功夫。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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