【Python】如何使用 Python 的 pprint库格式化和输出列表和字典

这两天, 田辛老师在进行几个OpenCV的项目, 涉及到大量的dict以及list内容的输出。 不多不说, python的print函数对于字典和列表的表示并不友好。 在此时,我们可以通过使用 Python 的标准库 pprint 模块,以干净的格式输出和显示列表和字典等对象。pprint 是“pretty-print”的缩写。

1 pprint的基本用法

普通print()函数将列表和字典的元素打印在一行上,没有换行符。例如,如下代码:

point_lst = [{'Name': '田辛', 'Age': 40, 'Points': [80, 20]}, {'Name': '张三', 'Age': 20, 'Points': [90, 10]},  
             {'Name': '李四', 'Age': 30, 'Points': [70, 30]}]
print(point_lst)
[{'Name': '田辛', 'Age': 40, 'Points': [80, 20]}, {'Name': '张三', 'Age': 20, 'Points': [90, 10]}, {'Name': '李四', 'Age': 30, 'Points': [70, 30]}]

进程已结束,退出代码0

这个输出其实是相当不友好的。 这个list但凡长一点看起来就非常的麻烦, 那么这种情况下使用pprint的方法就很方便:

pprint.pprint(point_lst)

输出的结果是:

[{'Age': 40, 'Name': '田辛', 'Points': [80, 20]},
 {'Age': 20, 'Name': '张三', 'Points': [90, 10]},
 {'Age': 30, 'Name': '李四', 'Points': [70, 30]}]

进程已结束,退出代码0

2 指定输出宽度(字符数):width

2.1 例子:

pprint.pprint(point_lst, width=40)

输出结果是:

[{'Age': 40,
  'Name': '田辛',
  'Points': [80, 20]},
 {'Age': 20,
  'Name': '张三',
  'Points': [90, 10]},
 {'Age': 30,
  'Name': '李四',
  'Points': [70, 30]}]

进程已结束,退出代码0

2.2 特殊说明

如果你给的width值特别大, 比如400, 那么这个时候pprint会和print没有区别。

pprint.pprint(point_lst, width=400)

输出结果是:

[{'Age': 40, 'Name': '田辛', 'Points': [80, 20]}, {'Age': 20, 'Name': '张三', 'Points': [90, 10]}, {'Age': 30, 'Name': '李四', 'Points': [70, 30]}]

进程已结束,退出代码0

2.3 默认换行

默认情况下width会自动检查列表或者字典的元素是否适合输出, 如果不适合的话,他们将被换行。

pprint.pprint(list(range(10)))  
pprint.pprint(list(range(1000000, 1000010)))

输出结果是:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1000000,
 1000001,
 1000002,
 1000003,
 1000004,
 1000005,
 1000006,
 1000007,
 1000008,
 1000009]

进程已结束,退出代码0

2.4 紧凑换行:compact=True

在默认换行中, 如果产生换行,列表的每个元素都会换行。 有的时候, 这种换行显示会过长。 在这种情况下, 可以使用紧凑换行。

pprint.pprint(list(range(1000000, 1000020)), compact=True)

输出结果是:

[1000000, 1000001, 1000002, 1000003, 1000004, 1000005, 1000006, 1000007,
 1000008, 1000009, 1000010, 1000011, 1000012, 1000013, 1000014, 1000015,
 1000016, 1000017, 1000018, 1000019]

进程已结束,退出代码0

3 指定要打印的元素的深度:depth

您可以指定要输出的元素的深度depth作为参数。这里的深度就是嵌套数据的深度。

pprint.pprint(point_lst, depth=1)  
pprint.pprint(point_lst, depth=2)

输出结果是:

[{...}, {...}, {...}]
[{'Age': 40, 'Name': '田辛', 'Points': [...]},
 {'Age': 20, 'Name': '张三', 'Points': [...]},
 {'Age': 30, 'Name': '李四', 'Points': [...]}]

进程已结束,退出代码0

查看上面的执行结果, 你会发现, 对于超出depth深度的部分, 会直接用...表示。

4 制定缩进的宽度:indent

通过设定indent可以指定缩进, 默认缩进为1, 读者可以仔细看刚的例子中, 第二行会有一个字符的缩进哦。

pprint.pprint(point_lst, width=4, indent=4)

输出结果是:

[   {   'Age': 40,
        'Name': '田辛',
        'Points': [   80,
                      20]},
    {   'Age': 20,
        'Name': '张三',
        'Points': [   90,
                      10]},
    {   'Age': 30,
        'Name': '李四',
        'Points': [   70,
                      30]}]

进程已结束,退出代码0

5 全部代码

老规矩, 全部代码奉上:

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
"""  
#-----------------------------------------------------------------------------  
#                     --- TDOUYA STUDIOS ---  
#-----------------------------------------------------------------------------  
#  
# @Project : di08-tdd-cdg-python-learning  
# @File    : pprint_learning.py  
# @Author  : tianxin.xp@gmail.com  
# @Date    : 2023/4/15 17:16  
#  
# pprint 示例程序  
#  
#--------------------------------------------------------------------------"""  
import pprint  
  
point_lst = [{'Name': '田辛', 'Age': 40, 'Points': [80, 20]}, {'Name': '张三', 'Age': 20, 'Points': [90, 10]},  
             {'Name': '李四', 'Age': 30, 'Points': [70, 30]}]  
  
print(point_lst)  
  
pprint.pprint(point_lst)  
  
pprint.pprint(point_lst, width=40)  
  
pprint.pprint(point_lst, width=400)  
  
pprint.pprint(list(range(10)))  
pprint.pprint(list(range(1000000, 1000010)))  
pprint.pprint(list(range(1000000, 1000020)), compact=True)  
  
pprint.pprint(point_lst, depth=1)  
pprint.pprint(point_lst, depth=2)  
  
pprint.pprint(point_lst, width=4, indent=4)
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

田辛 | 田豆芽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值