python - pprint 字典在多行

标签 python python-2.7 dictionary pprint

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

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

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

{'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}}

关于python - pprint 字典在多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20171392/

相关文章:

python - Tkinter 网格管理器(类型错误)

c++ - 使用字符串键初始化结构映射

python - 在 Tkinter 按钮内更新 Python 字典中的值

c++ - 为什么在 C++ 中遍历映射失败?

python - BeautifulSoup 在 Amazon EC2 机器上的行为有所不同

python - 尝试使用YouTube API每小时检查一次视频观看次数。可以使用Python存储数据还是需要PHP?

python - 将 csv 中的行拆分为 python 中的行

Python smtplib 没有属性 SMTP_SSL

python - web2py 添加用户名验证

python - 使用 xlwt 将 Python 列表写入 Excel 行