python 对Unicode解码

Posted feiquan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 对Unicode解码相关的知识,希望对你有一定的参考价值。

打印:

print(我喜欢你.encode(utf8))

得到Unicode编码:

b\\u6211\\u559c\\u6b22\\u4f60

 

将上面的编码赋值给str后解码:

#Unicode
s1=\\u6211\\u559c\\u6b22\\u4f60

#转为utf-8(明文)
print(s1.encode(utf8).decode(unicode_escape))
#转为utf-8编码
print(s1.encode(utf8).decode(unicode_escape).encode(utf8))

输出:

我喜欢你
b\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2\xe4\xbd\xa0

 

以上是关于python 对Unicode解码的主要内容,如果未能解决你的问题,请参考以下文章