_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?
add_filefor the image, andadd_fieldfor text..add_file("my_image", "go-next.png", open("c:/go-next.png", "rb"), "image/png")and for text.add_field("key", "text")utf-8(which is an encoding for text) you need a format in between. For example, using base64:file_data_b64 = b64encode(file_data).decode('utf-8')And then you can get back to the binary format when you save the file to avoid data loss:a_file.write(b64decode(file_data))Decoding with another text encoding , likelatin-1, before encoding toutf-8is dangerous because not every binary file (for example, PNG) would be suitable for text encoding (probablydata.decode('latin-1').encode("utf-8")is NOT what you want)