_f = open("c:/go-next.png", "rb")
data = _f.read()
_f.close()
data.encode("utf-8")
# Error: UnicodeDecodeError: file <maya console> line 1: ascii #
As you see I open a image file, and the data is type. But I have to convert it to utf-8. Maybe binary data has some extra char (or not), it conflict with conversion. Is there any way to solve it?
Encoding means converting strings to storable bytes.
And Decoding means converting bytes to readable strings.
The
data
in your code is already encoded.Image cannot be converted into something like charters in utf8.
Text encodings only apply to text. Do not attempt to use them on binary data.
You can always map a
str
tounicode
using thelatin-1
codec. Once you have aunicode
, you can always encode it inutf-8
:What you're trying to accomplish can probably be achieved by base64 encoding it.