0

I was trying to load a .json file. This file is an English word dictionary that contained a lot of Unicode like \u266f. By using encoding = "utf8" can not solve the error. Then I replaced all of the Unicode with UTF-8; but still, it shows the same error.


My code:
import json
data = json.load(open("data.json", encoding="utf8"))
print(data)


Result:
Traceback (most recent call last):
  File "E:\Python\Dictionary App\test.py", line 4, in <module>
    print(data)
  File "C:\Users\ahnaf\AppData\Local\Programs\Python\Python38-32\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u266f' in position 657370: character maps to <undefined>
[Finished in 0.32s]

The json file: data.json

Ahnaf
  • 33
  • 3
  • 9
  • 1
    The exception you're getting is related to the *output* of your data, not to loading it. Your console probably can't handle Unicode. To fix that you probably need to change how you're *running* Python, not what the code you're using does. – Blckknght Apr 19 '20 at 16:00
  • @blckknght Do you've any idea how can I solve this? – Ahnaf Apr 19 '20 at 16:02
  • there is a previous answer on this topic here: https://stackoverflow.com/questions/5419/python-unicode-and-the-windows-console – Plato77 Apr 19 '20 at 16:24

1 Answers1

-1

Try to include:

# -*- coding: utf-8 -*-

or

# coding: utf-8

in your python file header.

Exemple:

# coding: utf-8
import json

with open("data.json") as f:
    data = json.loads(f.read())

print(data)
Martin13
  • 109
  • 9
  • 1
    The coding header only has to do with the encoding of the source code in the file, it has nothing to do with how the Python interpreter does IO while running the program. – Blckknght Apr 19 '20 at 16:47