How to save Unicode codepoint as character, not codepoint in Python

874 views Asked by At

Is there a way to save a Unicode string into JSON that allows for Unicode codepoints to be replaced with their actual characters?

For instance, having a dict like this ported into JSON...:

dict1[u'N\u00e1utico'] = 2

...instead of having it dumped with the codepoint, could the key be dumped as the actual string?:

NĂ¡utico

Printing works fine for representing the characters, but saving I'm just lost on. Thanks.

2

There are 2 answers

2
g.d.d.c On BEST ANSWER

Any library that writes JSON is going to provide the unicode codepoint for characters that fall outside of the standard ASCII range, and any library that can read JSON (including browsers) are going to display it correctly. I'm not certain why you think you need the accented character when the string is represented in JSON, but you shouldn't, and as an interchange format providing the codepoint is the correct behavior.

1
bobince On

Do you mean include non-ASCII characters as raw characters, rather than the equivalent \u escapes? If so:

>>> print json.dumps({u'N\u00e1utico': 2}, ensure_ascii= False)
{"NĂ¡utico": 2}