python pprint嵌套字典换行每个嵌套?

Spa*_*ide 6 python

我有以下数据结构:

{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)

当我在python中使用pprint时,我得到了

{'row_errors': {'hello.com': {'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)

但是,我非常希望它像以下一样打印:

{'row_errors': 
    {'hello.com': 
        {'template': [u'This field is required.']}}}
Run Code Online (Sandbox Code Playgroud)

这可以通过pprint配置吗?(我更喜欢pprint,因为我在jinja模板中打印它).

mde*_*gis 5

就像评论中建议的wimjson.dumps()一样,您可以使用.

引用Blender的回答:

json模块已经使用 > 参数实现了一些基本的漂亮打印indent

>>> import json
>>>
>>> your_json = '["foo", {"bar":["baz", null, 1.0, 2]}]'
>>> parsed = json.loads(your_json)
>>> print json.dumps(parsed, indent=4, sort_keys=True)
[
    "foo", 
    {
        "bar": [
            "baz", 
            null, 
            1.0, 
            2
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)