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

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

相信我们都已经通过“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. ]
  15. 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模块,可以让打印任意数据结构更具可读性。

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=1, width=80, depth=None,
  2.  stream=None, *, compact=False, sort_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. ]
  17. 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的全部内容。

当前文章:让我们来谈谈python中的prettyprint和pprint
分享链接:http://www.shufengxianlan.com/qtweb/news36/337286.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联