Frage

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.

War es hilfreich?

Lösung

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.

Andere Tipps

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}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top