[JSON—Python] Handle escape characters in Json files

[JSON—Python] Handle escape characters in Json files

1. Introduction to the problem

  • Question: As shown in the figure below, the file name is test_data.json. Where it should be displayed as text, it is displayed as an escape character .
    insert image description here
  • Goal: I want to make the text display normally .

2. Solutions

  • ensure_ascii is set to False.
import json

with open('test_data.json', 'r', encoding="utf-8") as file:
    str = file.read()
    data = json.loads(str)

json_str = json.dumps(data, indent=4, ensure_ascii=False)	# ensure_ascii 设置为 False,默认为true,转义

with open('./test_data1.json', 'w', encoding="utf-8") as json_file:
        json_file.write(json_str)

3. Effect

insert image description here

Guess you like

Origin blog.csdn.net/qq_51392112/article/details/130476149