0

I am working on a json file with Chinese words in it. I try to read it in my machine with Python, but the Chinese characters just can't be correctly displayed. Here is the json file(a.json):

{
  "squadName": "哈囉",
  "homeTown": "Metro City",
  "formed": 2016,
  "secretBase": "Super tower",
  "active": true
}

Here is my code:

import json

storage = []
with open('a.json', 'r', encoding='utf-8') as f:
    data = json.load(f)
    print(data)

Here is the output:

{'squadName': '���o', 'homeTown': 'Metro City', 'formed': 2016, 'secretBase': 'Super tower', 'active': True}

Everything works properly except for the Chinese characters Does anyone know how to fix it?

I've searched so many solutions and I also set the 'encoding' parameter to utf-8 or utf-8-sig, but it still not working.

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Cody Gao
  • 3
  • 3
  • 1
    Have you confirmed the encoding of the json file? Maybe its not utf-8. – topsail May 26 '23 at 03:47
  • What python version are you using ? – rr_goyal May 26 '23 at 03:56
  • 1
    If this is an input encoding problem, we need to see the actual encoded bytes from the input JSON. However, the answer you got about an _output_ encoding problem seems like a better speculation with the information you have provided (and is a very common FAQ to boot; the simplest answer is "don't use Windows" but since that is unacceptable to many beginners, there is a plethora of workarounds). See also https://meta.stackoverflow.com/questions/379403/problematic-questions-about-decoding-errors – tripleee May 26 '23 at 04:05

2 Answers2

1

Your code works just fine. However, you need to ensure that your output handler is capable of properly displaying these characters.

Façade
  • 11
  • 3
0

Yes, you don't need to fix anything. Your code is just fine.

As you can see below:

(this is Pycharm IDE)

(CMD terminal) enter image description here

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24