Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading