有什麼簡單的方法可以讓我將二進制文件的內容作爲二進制字符串讀取,將其轉換爲普通(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