问题
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