skip to Main Content

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.

2

Answers


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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search