有什么简单的方法可以让我将二进制文件的内容作为二进制字符串读取,将其转换为普通(utf-8)字符串,对其进行一些操作,将其转回成二进制字符串,并将其写入二进制文件?我试图做的简单的东西:通过python 3字节文件转换
a_file = open('image1.png', 'rb')
text = b''
for a_line in a_file:
text += a_line
a_file.close()
text2 = text.decode('utf-8')
text3 = text2.encode()
a_file = open('image2.png', 'wb')
a_file.write(text3)
a_file.close()
,但我得到“的Unicode不能在位置解码字节......”
我在做什么可怕的错误?
为什么你认为一个PNG文件将包含文本? –
不知道你想完成什么,但[这个答案](http://stackoverflow.com/a/22621777/355230)对另一个问题可能会有所帮助。 – martineau