POSTS / Python Non-ASCII JSON Dumping Issues

Published: 2023-12-06

When you write to a file that was opened in text mode, Python encodes the string for you. The default encoding is ascii, which generates the error you see.

from UnicodeEncodeError: ‘ascii’ codec can’t encode

use this method to specify encoding when open a file:

# example
with open(file_path, "r", encoding="UTF-8") as file:
    file_contents += "\n".join(file.readlines())

i met one error today when i tried to dump a non-ascii JSON to file:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)

i was using ensure_ascii=False then still got that error.

i realized it still need to use encoding="UTF-8" while opening a file even for writing (with help of UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0’ in position 20: ordinal not in range(128)) and it really works:

with open("one file", "w", encoding="UTF-8") as file:
    json.dump(one_object, file, ensure_ascii=False)