使用Python在字符串中转义字符

Spi*_*ike 7 python unicode json escaping posterous

我做了一个JSON请求,它给了我一个使用Unicode字符代码的字符串,如下所示:

s = "\u003Cp\u003E"
Run Code Online (Sandbox Code Playgroud)

我想将其转换为:

s = "<p>"
Run Code Online (Sandbox Code Playgroud)

在Python中执行此操作的最佳方法是什么?

注意,这是与问题相同的问题,仅在Python中除了Ruby之外.我也在使用Posterous API.

Ign*_*ams 17

>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'
Run Code Online (Sandbox Code Playgroud)


Tho*_*ers 13

如果数据来自JSON,json模块应该已经为您解码了这些转义:

>>> import json
>>> json.loads('"\u003Cp\u003E"')
u'<p>'
Run Code Online (Sandbox Code Playgroud)