9

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.

ti7
  • 16,375
  • 6
  • 40
  • 68
Spike
  • 5,040
  • 5
  • 32
  • 47

3 Answers3

18

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>'
Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • I wish I could mark both these answers as correct. Thanks for your help! – Spike Apr 05 '11 at 16:58
  • @Spike: if it is JSON input, this is the real right answer. Python string literals are not completely compatible with JSON string literals and `unicode-escape` can return the wrong results for characters outside the Basic Multilingual Plane (which JS/JSON store as surrogates but Python might not). – bobince Jul 03 '11 at 08:22
17
>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

EDIT: The original question "Unescaping Characters in a String with Python" did not clarify if the string was to be written or to be read (later on, the "JSON response" words were added, to clarify the intention was to read).

So I answered the opposite question: how to write JSON serialized data dumping them to a unescaped string (rather than loading data from the string).

My use case was producing a JSON file from my own data dictionary, but the file contained scaped non-ASCII characters. So I did it like this:

with open(filename,'w') as jsonfile:
    jsonstr = json.dumps(myDictionary, ensure_ascii=False)
    print(jsonstr)          # to screen
    jsonfile.write(jsonstr) # to file

If ensure_ascii is true (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is false, these characters will be output as-is.

Taken from here: https://docs.python.org/3/library/json.html

abu
  • 422
  • 7
  • 14
  • heads-up: I edited the (wow almost a decade-old) Question with some hindsight and your Answer to remove some of the extra intro - feel free to revert if you don't think this was appropriate! – ti7 Feb 10 '21 at 21:03
  • thanks @ti7 I prefer to clarify this answer was written before your change of the original title – abu Feb 13 '21 at 16:56