Unescaping Characters in a JSON response string
Asked Answered
A

3

11

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.

Antalkali answered 5/4, 2011 at 16:19 Comment(0)
T
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>'
Terribly answered 5/4, 2011 at 16:26 Comment(2)
I wish I could mark both these answers as correct. Thanks for your help!Antalkali
@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).Kimbro
H
17
>>> "\\u003Cp\\u003E".decode('unicode-escape')
u'<p>'
Heal answered 5/4, 2011 at 16:23 Comment(0)
N
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

Notarize answered 10/2, 2021 at 20:55 Comment(2)
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!Wedge
thanks @Wedge I prefer to clarify this answer was written before your change of the original titleNotarize

© 2022 - 2024 — McMap. All rights reserved.