json.dump(json_obj, f, ensure_ascii=False), why open json and write 1 line, how to format multi-line display?

When Python uses its own json library to write a json file, why does the written json file only have one line after opening. How to make the json file display in multiple lines in a formatted way? Improve readability?

When writing a JSON file, use json.dump to serialize the JSON object into the file. The ensure_ascii=False parameter tells the Python interpreter not to escape non-ASCII characters (escape), which makes non-ASCII characters be the original Unicode characters when writing JSON text.

If no indent parameter is specified when opening the json file, the default output is one line, and the entire JSON text is written on the same line. If you want the JSON text output format to be more beautiful and easy to read, you can specify the indentation parameter indent, such as:

with open('data.json', 'w', encoding='utf-8') as f:
    json.dump(json_obj, f, indent=4, ensure_ascii=False)

Among them, indent=4 means to format the JSON text with four spaces as the indentation unit. In this way, the output JSON text will be displayed according to the corresponding indentation format.

Guess you like

Origin blog.csdn.net/programmer589/article/details/130297518