evo*_*ion 3 python string converter utf-8 unicode-escapes
我使用python 2.7,我从服务器接收一个字符串(不是在unicode!).在该字符串中,我找到了带有unicode转义序列的文本.例如这样:
<a href = "http://www.mypage.com/\u0441andmoretext">\u00b2<\a>
Run Code Online (Sandbox Code Playgroud)
我如何将这些转换\uxxxx回utf-8?我找到的答案要么是处理要么是&#要求eval(),这对我来说太慢了.我需要一个包含这种序列的任何文本的通用解决方案.
编辑:这
<\a>是一个错字,但我也想要对这种拼写错误进行容忍.应该只有反应\u
示例文本用适当的python语法表示如下:
"<a href = \"http://www.mypage.com/\\u0441andmoretext\">\\u00b2<\\a>"
Run Code Online (Sandbox Code Playgroud)
所需的输出是适当的python语法
"<a href = \"http://www.mypage.com/\xd1\x81andmoretext\">\xc2\xb2<\\a>"
Run Code Online (Sandbox Code Playgroud)
尝试
>>> s = "<a href = \"http://www.mypage.com/\\u0441andmoretext\">\\u00b2<\\a>"
>>> s.decode("raw_unicode_escape")
u'<a href = "http://www.mypage.com/\u0441andmoretext">\xb2<\\a>'
Run Code Online (Sandbox Code Playgroud)
然后你可以照常编码到utf8.