Python 2.7如何打印unicode转义格式字符串



我知道python中的unicode字符串,就像u"blablabla"。它应该以"u"开头。但我在api中找到了一些unicode之类的字符串,比如

{"ret":"100015","msg":"u5bc6u7801u8f93u5165u9519u8befuff0cu8fd8u53efu4ee5u8f93u51654u6b21","msg_new":"u5bc6u7801u8f93u5165u9519u8bef<br>u8fd8u53efu4ee5u8f93u5165<b>4</b>u6b21"}

字符串不以"u"字符开头,所以我想知道如何处理这些字符串?

对于给定的str(在python 3中,bytes),字符以uHHHH形式编码(unicode字符转义序列),可以使用unicode-escape编解码器进行解码。

>>> s = "u5bc6u7801u8f93u5165u9519u8befuff0cu8fd8u53efu4ee5u8f93u51654u6b21"
>>> type(s)
<type 'str'>
>>> r = s.decode("unicode-escape")
>>> type(r)
<type 'unicode'>
>>> print(r)
密码输入错误,还可以输入4次

最新更新