Home > database >  Does the encoding make sense in json or yaml files in python?
Does the encoding make sense in json or yaml files in python?

Time:04-07

I've tried to save this dictionary in json or yaml file:

d = {'name': 'María Gómez', 'message': '¡Es una caña atómica!'}
with open(file, 'wt', encoding='iso8859-1') as file:
   json.dump(d, file)

However, the file content is not in that encoding and the non-ascii characters are escaped within json or yaml files. Therefore, I assume that the encoding file doesn't make sense. Or is there some languages or characters that the encoding could make sense with this type of files?

CodePudding user response:

import json
d = {'name': 'María Gómez', 'message': '¡Es una caña atómica!'}
json.dumps(d)
# '{"name": "Mar\\u00eda G\\u00f3mez", "message": "\\u00a1Es una ca\\u00f1a at\\u00f3mica!"}'
json.dumps(d, ensure_ascii=False)
# '{"name": "María Gómez", "message": "¡Es una caña atómica!"}'

Explanation (valid for json.dumps as well as for json.dump): JSON encoder and decoder basic usage:

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.

Read Character Encodings as well.

  • Related