python-将二进制数据转换为utf-8 [英] python - convert binary data to utf-8

查看:601
本文介绍了python-将二进制数据转换为utf-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_f = open("c:/go-next.png", "rb")
data = _f.read()
_f.close()
data.encode("utf-8")

# Error: UnicodeDecodeError: file <maya console> line 1: ascii # 

如您所见,我打开了一个图像文件,并且数据是类型.但是我必须将其转换为utf-8. 也许二进制数据有一些额外的字符(或没有),它与转换冲突.有什么办法解决吗?

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?

推荐答案

文本编码仅适用于文本.请勿尝试在二进制数据上使用它们.

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

这篇关于python-将二进制数据转换为utf-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆