pprint字典多行 [英] pprint dictionary on multiple lines

查看:94
本文介绍了pprint字典多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图得到一本漂亮的字典,但我没有运气:

I'm trying to get a pretty print of a dictionary, but I'm having no luck:

>>> import pprint
>>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}}
>>> pprint.pprint(a)
{'first': 123, 'second': 456, 'third': {1: 1, 2: 2}}

我希望输出在多行中,如下所示:

I wanted the output to be on multiple lines, something like this:

{'first': 123,
 'second': 456,
 'third': {1: 1,
           2: 2}
}

pprint 可以做到吗?如果不是,那么它是哪个模块?我正在使用 Python 2.7.3 .

推荐答案

使用width=1width=-1:

In [33]: pprint.pprint(a, width=1)
{'first': 123,
 'second': 456,
 'third': {1: 1,
           2: 2}}

这篇关于pprint字典多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆