python - 写入行数超过 45 字节的二进制文件:binascii.Error: 一次最多 45 字节
问题描述
我的目标是读取二进制文件并将其转换为文本。我的代码是:
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))
推荐阅读
- python - 在 GridSearchCV 评分中使用具有预测概率的自定义损失时,make_scorer 如何工作?
- sql - 使用 Case 语句的具有多列的 Order By 子句
- python - 我无法从 Django 的服务层运行函数
- java - Logback 自定义 appender 通过 Open Telemetry 将日志推送到 ElasticSearch
- python - Holoview、Panel、Bokeh 版本,使用时不会出现导入错误
- python - 线性回归拟合给出了奇怪的预测
- javascript - Next.js 导出 - 自定义图像加载器不工作
- nginx - 如何动态设置 Nginx 缓存限制?
- kotlin - 在函数调用 kotlin 结束时使用大括号
- html - 在列表中设置不同的样式