Unescaping Characters in a String with Python

纵饮孤独 提交于 2019-12-01 02:05:10

问题


I made a JSON request that gives me a string that uses Unicode character codes that looks like:

s = "\u003Cp\u003E"

And I want to convert it to:

s = "<p>"

What's the best way to do this in Python?

Note, this is the same question as this one, only in Python except Ruby. I am also using the Posterous API.


回答1:


If the data came from JSON, the json module should already have decoded these escapes for you:

>>> import json
>>> json.loads('"\u003Cp\u003E"')
u'<p>'



回答2:


>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'


来源:https://stackoverflow.com/questions/5555063/unescaping-characters-in-a-string-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!