想获得漂亮的格式化字符串后输出,可以使用json.dumps() 的indent 参数。它会使得输出和pprint() 函数效果类似
>>> data {'age': 4, 'name': 'niuniuche', 'attribute': 'toy'} >>> import json >>> print(json.dumps(data)) {"age": 4, "name": "niuniuche", "attribute": "toy"} >>> print(json.dumps(data,indent=3)) { "age": 4, "name": "niuniuche", "attribute": "toy" } >>>