Python 3.4解码字节 [英] Python 3.4 decode bytes

查看:89
本文介绍了Python 3.4解码字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用python编写文件,但在写文件之前我找不到解码字节对象的方法,基本上,我试图解码此字节字符串:

I am trying to write a file in python, and I can't find a way to decode a byte object before writing the file, basically, I am trying to decode this bytes string:

Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s

这就是我要恢复的原始文本:

into this, which is the original text I'm trying to recover:

Les évadés

我尝试使用.decode('utf- 8')和encode('utf-8'),但似乎无济于事...

I tried using the .decode('utf-8') and encode('utf-8') but nothing seems to work...

我总是得到 LesévadÃss 结果...
我正在使用python 3.4.3

I always get Les évadés as a result... I am using python 3.4.3

任何人都可以提供帮助吗?

Anyone can help?

推荐答案

如果您想要Python 3解决方案:

And if you want a Python 3 solution:

b = b'Les \xc3\x83\xc2\xa9vad\xc3\x83\xc2\xa9s'
u = b.decode('utf-8').encode('latin-1').decode('utf-8')
print(u)
# Les évadés

这篇关于Python 3.4解码字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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