Skip to content Skip to sidebar Skip to footer

Python - Convert Binary Data To Utf-8

_f = open('c:/go-next.png', 'rb') data = _f.read() _f.close() data.encode('utf-8') # Error: UnicodeDecodeError: file line 1: ascii # As you see I open a ima

Solution 1:

You can always map a str to unicode using the latin-1 codec. Once you have a unicode, you can always encode it in utf-8:

data.decode('latin-1').encode("utf-8")

Solution 2:

Text encodings only apply to text. Do not attempt to use them on binary data.

Solution 3:

What you're trying to accomplish can probably be achieved by base64 encoding it.

importbase64encoded= base64.b64encode(image_binary_data)

Solution 4:

Encoding means converting strings to storable bytes. And Decoding means converting bytes to readable strings.

The data in your code is already encoded.

Solution 5:

Image cannot be converted into something like charters in utf8.

Post a Comment for "Python - Convert Binary Data To Utf-8"