json.dumps()及indent参数

json.dumps() 使字典类型漂亮的输出
indent参数决定添加几个空格

cla_dict = {“0”: “daisy”, “1”: “dandelion”, “2”: “roses”, “3”: “sunflowers”, “4”: “tulips”}
cla_dict
{‘0’: ‘daisy’, ‘1’: ‘dandelion’, ‘2’: ‘roses’, ‘3’: ‘sunflowers’, ‘4’: ‘tulips’}

import json
print(json.dumps(cla_dict))
{“0”: “daisy”, “1”: “dandelion”, “2”: “roses”, “3”: “sunflowers”, “4”: “tulips”}

print(json.dumps(cla_dict, indent=4))
{
“0”: “daisy”,
“1”: “dandelion”,
“2”: “roses”,
“3”: “sunflowers”,
“4”: “tulips”
}

print(json.dumps(cla_dict, indent=20))
{
“0”: “daisy”,
“1”: “dandelion”,
“2”: “roses”,
“3”: “sunflowers”,
“4”: “tulips”
}

你可能感兴趣的:(pyhon,json)