【发布时间】:2019-02-26 13:33:36
【问题描述】:
我正在使用 python3 以二进制模式打开图像 然后在特定标记处拆分该数据 (\xff\xda)
该标记之后的所有内容都存储在一个变量中 我想用 e's 替换所有 a's
但我在将二进制数据转换为字符串时遇到了麻烦:
UnicodeDecodeError : 'ascii' 编解码器无法在位置解码字节 0xe6 13: 序数不在范围内(128)
with open(filein, "rb") as rd:
with open(fileout,'wb') as wr:
img = rd.read()
if img.find(b'\xff\xda'): ## ff da start of scan
splitimg = img.split(b'\xff\xda', 1)
wr.write(splitimg[0])
scanimg = splitimg[1]
scanglitch = ""
scanimg = scanimg.encode()
for letter in scanimg :
if letter not in 'a':
scanglitch += letter
else :
scanglitch += 'e'
print(scanimg)
wr.write(b'\xff\xda')
content = scanglitch.decode()
wr.write(content)
encode() 和 decode() 不正确吗 将二进制数据转换为字符串并返回? 谢谢
【问题讨论】:
-
请也向我们显示回溯,以便我们知道是哪一行给出了该错误。
-
您确定首先需要转换为字符串吗?
b"this is a byte string".replace(b"a", b"e")可以将“a”替换为“e”,而无需使用字符串。 -
抱歉,回溯错误来自带有“scanimg = scanimg.encode()”的行,由于行号不匹配,不确定如何将其添加到我的帖子中
标签: python python-3.x jpeg