python - convert binary data to utf-8
15,073
Solution 1
Text encodings only apply to text. Do not attempt to use them on binary data.
Solution 2
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 3
What you're trying to accomplish can probably be achieved by base64 encoding it.
import base64
encoded = base64.b64encode(image_binary_data)
Related videos on Youtube
Author by
Hyun-geun Kim
Updated on June 04, 2022Comments
-
Hyun-geun Kim almost 2 years
_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?
-
yantrab about 11 yearsI don't mean to be blunt, but converting a PNG to UTF8 doesn't make any sense. PNG is an image format. UTF8 is a text encoding. Can you explain more what it is you are trying to do?
-
Esailija about 11 years@Hyun-geunKim using code in that link, you would
add_file
for the image, andadd_field
for text..add_file("my_image", "go-next.png", open("c:/go-next.png", "rb"), "image/png")
and for text.add_field("key", "text")
-
-
Ignacio Vazquez-Abrams about 11 yearsWhile this is true, the result has no meaning.
-
unutbu about 11 yearsWithout meaning? A supercomputer was once asked, "What is the meaning of life?" I'm pretty sure it answered,
'42'.decode('latin1').encode('utf-8')
. :-) -
Hyun-geun Kim about 11 yearsYes, I and my colleague set up another POST module.
-
yingted over 9 years@IgnacioVazquez-Abrams The
latin-1
codec maps codepoints "0-255 to the bytes 0x0-0xff": docs.python.org/2/library/codecs.html#encodings-and-unicode So, this does exactly what you'd expect. -
Ignacio Vazquez-Abrams over 9 years@yingted: I'm pretty sure that converting 0xd0 to 0xc3 0x90 atc. will corrupt the image, which is hardly what you'd want.
-
yingted over 9 years@IgnacioVazquez-Abrams I just hit this. I have to transfer a blob across a UTF-8 channel, but I don't have a base64 decoder on the receiving side. This would be the correct answer if the question made any sense.