Writing a binary file with lines more than 45 bytes: binascii.Error: At most 45 bytes at once

Writing a binary file with lines more than 45 bytes: binascii.Error: At most 45 bytes at once

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

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 之外还有其他我可以使用的模块吗?谢谢!

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))