Unescaping Characters in a String with Python

2020-03-17 04:30发布

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.

2条回答
叼着烟拽天下
2楼-- · 2020-03-17 04:45
>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'
查看更多
Luminary・发光体
3楼-- · 2020-03-17 04:50

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>'
查看更多
登录 后发表回答