让我们来谈谈python中的prettyprint和pprint

开发 后端
python中的pprint模块负责以合适的格式打印便于阅读的行块。它使用换行和缩进以明确的方式打印数据。

当你开始学习python编程的时候,你做的第一件事是什么?

[[344825]]

相信我们都已经通过“Hello World”程序开始了我们的python之旅。在python中,它可以在一行中完成:

  1. print(“Hello World”) 

但是,在使用print()函数打印字典、列表或任何其他复杂数据类型时,您是否遇到过这种痛苦呢?由于不适当的缩进问题,我们经常在python嵌套数据结构的输出中遇到可读性方面的困难。

让我们在这里试试代码:

  1. coordinates = [ 
  2.  { 
  3.  “name”: “Location 1”, 
  4.  “gps”: (29.008966, 111.573724) 
  5.  }, 
  6.  { 
  7.  “name”: “Location 2”, 
  8.  “gps”: (40.1632626, 44.2935926) 
  9.  }, 
  10.  { 
  11.  “name”: “Location 3”, 
  12.  “gps”: (29.476705, 121.869339) 
  13.  } 
  14. print(coordinates) 

以上代码的输出:

  1. [{‘gps’: (29.008966, 111.573724), ‘name’:  
  2. ‘Location 1’}, {‘gps’: (40.1632626, 44.2935926),  
  3. ‘name’: ‘Location 2’}, {‘gps’: (29.476705, 121.869339),  
  4. ‘name’: ‘Location 3’}] 

我们可以看到,上面的代码不容易读懂,也不美观。

如果解决这个问题呢?

Python附带pprint模块,可以让打印任意数据结构更具可读性。

让我们来谈谈python中的prettyprint

pprint是什么?

python中的pprint模块负责以合适的格式打印便于阅读的行块。它使用换行和缩进以明确的方式打印数据。

pprint与print有何不同?

print()是python中的一个简单函数,用于在屏幕上向用户显示指定的消息。但通常,如果我们使用python打印一个字典、列表或任何其他复杂的函数,我们会发现读取打印出来的语句是模棱两可的。它包括内置的对象、文件、套接字、类或实例,这些不能用Python常量表示。

然后,“pprint”模块可以帮助您。

它将对象格式化为可读的格式,每行都有适当的宽度。它带有可调节的宽度限制,以使它更容易为用户。它将所有元素转换为可以用Python常量表示的字符串,并以美观的格式打印它们。pprint函数返回可以在解释器中作为输入运行的输出。而且,通过解析字符串更容易将其转换回该类型。

那么,让我们深入pprint…

在python文件的顶部导入库pprint:

  1. import pprint 

现在,我们可以使用.pprint()对象或实例化我们自己的pprint对象PrettyPrinter()。

  1. pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847]) 
  2. # Instantiating pprint object 
  3. my_pprint = pprint.PrettyPrinter() 
  4. my_pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847]) 

两个打印函数给出的结果是一致的

  1. ['Radha', 1, 'Hari', 'Simesh', 25, 847]  
  2. ['Radha', 1, 'Hari', 'Simesh', 25, 847] 

那么,pprint()和PrettyPrinter()之间的区别是什么?

如果需要调整宽度约束或其他参数,则显式地构造PrettyPrinter对象。

语法:

  1. class pprint.PrettyPrinter(indent=1width=80depth=None
  2.  stream=None, *, compact=Falsesort_dicts=True

pprint()方法使用库的默认设置,而在创建PrettyPrinter()对象时,我们可以更改库的默认配置。这就是二者之间的区别。​

让我们通过几个例子来理解:

深度参数决定多远一个Python PrettyPrinter递归嵌套结构:

  1. tuple1 = ('spam', ('eggs', ('lumberjack', ('knights',  
  2. ('ni', ('dead',('parrot', ('fresh fruit',)))))))) 
  3. # Using PrettyPrinter 
  4. pp = pprint.PrettyPrinter(depth=6) # default configuration  
  5. # of depthbeing none is changed to depth = 6 
  6. # Now it will print till depth of six brackets 
  7. pp.pprint(tuple1) 
  8. #Using only pprint() object 
  9. pprint.pprint(pprint.pprint(tuple1, depth=6)) 
  10. pprint.pprint(tuple1) 

以上代码的输出:

  1. ('spam', ('eggs', ('lumberjack', ('knights', ('ni',  
  2. ('dead', (...)))))))  
  3. ('spam', ('eggs', ('lumberjack', ('knights', ('ni',  
  4. ('dead', (...))))))) 
  5. ('spam', 
  6.   ('eggs', 
  7.    ('lumberjack', ('knights', ('ni', ('dead',  
  8.    ('parrot', ('fresh fruit',)))))))) 

你能注意到其中的区别吗?

我们看到,当tuple1打印使用深度= 6,之后六个椭圆打印的时候是没有更多的数据显示而只使用pprint.pprint(),那么所有的数据显示。

设置不存储在.pprint()中,即默认设置保持不变,而在PrettyPrinter()中,设置或更改是存储的。这里存储的是depth = 6。

使用宽度参数,我们可以选择输出将打印多少列。默认宽度是80,也就是80个字符,但是你可以改变它。

现在,让我们看看在几个内置函数中使用pprint()比print()函数的主要区别和好处。

  1. from pprint import pprint # We can directly call the method  
  2. # pprint() using it 
  3. coordinates = [ 
  4.  { 
  5.  “name”: “Location 1”, 
  6.  “gps”: (29.008966, 111.573724) 
  7.  }, 
  8.  { 
  9.  “name”: “Location 2”, 
  10.  “gps”: (40.1632626, 44.2935926) 
  11.  }, 
  12.  { 
  13.  “name”: “Location 3”, 
  14.  “gps”: (29.476705, 121.869339) 
  15.  } 
  16. pprint(coordinates) 

输出:

  1. [{'gps': (29.008966, 111.573724), 'name': 'Location 1'}, 
  2.  {'gps': (40.1632626, 44.2935926), 'name': 'Location 2'}, 
  3.  {'gps': (29.476705, 121.869339), 'name': 'Location 3'}] 

这就是本文中关于python中的pprint的全部内容。

​英文原文:

https://medium.com/dev-genius/lets-talk-about-prettyprint-or-pprint-in-python-ddda1fa4cf0b​

 

责任编辑:赵宁宁 来源: Python学会
相关推荐

2018-03-13 15:33:14

虚拟化备份虚拟机

2021-12-16 12:01:21

区块链Libra货币

2013-11-27 09:38:31

云计算虚拟化

2015-12-30 10:33:12

2012-04-14 20:47:45

Android

2012-08-23 09:35:46

2021-08-05 05:02:04

DPU数据中心Pensando

2018-08-15 14:02:19

ODCCIT领域液冷

2019-06-13 16:12:35

Android QGoogle智能手机

2014-09-29 09:00:49

阿里云阿里云开发者大会

2021-11-04 18:15:55

下载上传浏览器

2020-11-27 06:44:22

原子加锁x86

2017-11-02 14:39:54

2023-09-06 08:57:33

NLTK自然语言处理工具

2017-11-02 15:28:52

2023-12-18 08:00:42

JavaScrip框架Lit

2021-01-20 15:31:00

区块链比特币数字货币

2015-08-03 10:10:29

2010-01-04 14:37:46

Linux Ubunt

2021-10-20 14:04:10

代码注释接口
点赞
收藏

51CTO技术栈公众号