-2

I have a JSON file with the following content: {'好': {'word': '好'}}. It is encoded in UTF-8.

I'm using the following Python code to print the content:

import json

with open(f'chinese_character.json', 'r', encoding="utf-8") as f:
        contents = json.load(f)
        print(contents)

It returned {'好': {'word': '好'}}, which is not desired. How can I have it printed {'好': {'word': '好'}}?

wovano
  • 4,543
  • 5
  • 22
  • 49
  • 2
    That _is_ utf-8, that `好` is the utf-8 byte sequence for the CJK ideograph 好, you just didn't open it as utf-8 in whatever text/code editor you're using to look at the file? (Or printed it in a utf-8 context) – Mike 'Pomax' Kamermans Apr 14 '23 at 05:00
  • 1
    Your file is not JSON. JSON uses double quotes, not single quotes. – Mark Rotteveel Apr 14 '23 at 08:32
  • 1
    It is not advised to edit your question to include an answer. Instead, you should [accept](https://stackoverflow.com/help/accepted-answer) the answer that you feel is most appropriate to show it has answered your question. – sbottingota Apr 14 '23 at 16:15
  • What operating system are you using? What is the encoding of your terminal? What is the output of `python -c "import sys; print(sys.stdout.encoding)"`? If the JSON file is really UTF-8, your code is reading it correctly. The problem is likely that your terminal has a different encoding. – wovano Apr 17 '23 at 19:09
  • Not sure if this is the best question/answer, but it might help: [How to print utf-8 to console with Python 3.4 (Windows 8)?](https://stackoverflow.com/questions/25127673/how-to-print-utf-8-to-console-with-python-3-4-windows-8) – wovano Apr 17 '23 at 19:11

1 Answers1

-1

To read chinese chars from json, you can do:

import json

with open("filename.json", encoding="utf-8") as f:
  data = json.load(f)

By specifying the encoding, you can read chinese characters. Then you can print like:

print(data)
print(data["hao"])

etc.

To write chinese characters to json, it's pretty similar:


import json
# define variable data here
"""
data = {
    "hao": "好"
}
"""

with open("filename.json", "w", encoding="utf-8") as f:
    json.dump(data, f)

I hope this helps.

sbottingota
  • 533
  • 1
  • 3
  • 18
  • OP already know how to read the UTF-8 file (encoding is correctly specified). And the question seems to be how to `print` it, not how to write it to a file. – wovano Apr 17 '23 at 19:14