python-3.x - 二进制模式下的 Python 3 open() 未写入正确的内容
问题描述
所以基本上,我在使用 python3 写入字节时遇到问题。我已通过使用 print 确保内容符合预期,但它仍在写入不正确的内容。它基本上将第一行复制了 4 次。我正在使用拉丁语1。本质上,我在 b 字符串中包含的唯一原始字节是0x80
和0x81
(即\x80
和\x81
)。"wb"
我在模式下使用 building open() 函数编写它。有人知道发生了什么吗?
编辑:这是用于创建文件的主要方法。如前所述,fs 是以 wb 模式打开的文件,它按预期打印。
def assemble_file(self, fs):
"must call lex_from_asm_text before assembling"
self.bin_raw_content += b"ROSAPYVM\x81"
for x in self._asm_stripped:
try:
bcode = bytes([ops.__dict__[x[0]]])
print(bcode)
print([x[1:]])
self.bin_raw_content += b"\x00"
self.bin_raw_content += bcode
for i in x[1:]:
self.bin_raw_content += b"\x80"
print(i.encode("latin-1"))
self.bin_raw_content += i.encode("latin-1")
except IndexError:
pass
print(self.bin_raw_content)
fs.write(self.bin_raw_content)
解决方案
推荐阅读
- java - Vaadin 14 中的 React 组件
- python - 如何在组和子组中嵌套异步任务?
- python - 如何使用 MySQLdb 库通过 python 脚本执行 MySQL 查询?
- r - 使用 expand.grid 但保留命名字符值
- r - 需要 CentOS 脚本中的 SED 命令帮助以在 RStudio 包管理器中自动更新策划的 CRAN
- request - MOLAP 中的维度失败“高级关系引擎中的错误。[Teradata 数据库] [3134] 请求被 ABORT SESSION 命令中止
- javascript - 未捕获的 ReferenceError:io 未定义。我正在制作一个视频聊天应用程序
- amazon-iam - CodePipeline 在创建 CodeBuildAction 角色之前执行
- javascript - 比较 javascript 中井字游戏的行数
- python-3.x - 使用 crontab 运行 python 脚本