I have a list which contains dictionaries. Each dictionary has an attribute description which should contain a string. When I put this string into the dictionary, I know that it has the right encoding (when printed in the terminal, it looks good). Also, if I print the value of description after it has been set, the string still looks good.
Then I use JSONEncoder.encode(myList) and save I this JSON string to a file using the code below:
file_obj = open("file.txt", "w")
file_obj.write(text)
file_obj.close()
Then, when I open the file characters are shown wrong. E.g. \u00e5, \u00f8d and \u00e6.
Does anyone have an idea why this is and how I can fix this?
JSON encoding is designed to create a string that can be loaded via JSON decoding, which is exactly what it’s doing. “What it looks like when you open the file” is not the same as “what it will look like if you run it through a JSON decoder” – the JSON decoder will also decode the
\u####escapes.If you want the file to contain raw unicode, rather than
\u####escapes, don’t use a JSON encoder; that’s not what it’s designed for.