python 使用json.dumps() 的indent 参数,获得漂亮的格式化字符串后输出

想获得漂亮的格式化字符串后输出,可以使用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"
}
>>>

 

posted @ 2019-02-27 16:33  筱筱的春天  阅读(13235)  评论(0编辑  收藏  举报