编写一个行数超过45个字节的二进制文件:binascii.Error:一次最多45个字节

时间:2019-03-05 00:22:13

标签: python byte binascii

我的目标是读取二进制文件并将其转换为文本。我的代码是:

def binary_to_text(self,file_name):
  open_file = open(file_name,"rb")
  with open("Binary2Text.txt", "a") as the_file:
    for line in open_file:
      the_file.write(binascii.b2a_uu(line))

我收到此错误:

binascii.Error: At most 45 bytes at once

是否有解决此问题的方法,或者除了binascii之外还有其他模块可以使用吗?谢谢!

1 个答案:

答案 0 :(得分:1)

binascii.b2a_uu方法是用于执行uuencode的低级函数,其中该算法将输入的文本编码为45字节的块,这就是为什么输入限制为45字节的原因。

除非您尝试自己实现uuencode,否则应仅使用uu.encode方法:

import uu    
def binary_to_text(self, file_name):
    with open("Binary2Text.txt", "a") as the_file:
        the_file.write(uu.encode(file_name))