10

让我们来谈谈python中的prettyprint

 3 years ago
source link: http://developer.51cto.com/art/202010/627777.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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

print(“Hello World”)

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

让我们在这里试试代码:

coordinates = [ { “name”: “Location 1”, “gps”: (29.008966, 111.573724) }, { “name”: “Location 2”, “gps”: (40.1632626, 44.2935926) }, { “name”: “Location 3”, “gps”: (29.476705, 121.869339) }]print(coordinates)

以上代码的输出:

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

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

如果解决这个问题呢?

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

3YVFzuI.jpg!mobile

pprint是什么?

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

8352e32a6cf4a952730b5a088b093d8d.jpeg

pprint与print有何不同?

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

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

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

ieIJnue.jpg!mobile

那么,让我们深入pprint…

在python文件的顶部导入库pprint

import pprint

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

pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])# Instantiating pprint objectmy_pprint = pprint.PrettyPrinter()my_pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])

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

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

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

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

语法:

class pprint.PrettyPrinter(indent=1, width=80, depth=None,​ stream=None, *, compact=False, sort_dicts=True)

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

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

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

tuple1 = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',('parrot', ('fresh fruit',))))))))# Using PrettyPrinterpp = pprint.PrettyPrinter(depth=6) # default configuration # of depthbeing none is changed to depth = 6# Now it will print till depth of six bracketspp.pprint(tuple1)#Using only pprint() objectpprint.pprint(pprint.pprint(tuple1, depth=6))pprint.pprint(tuple1)

以上代码的输出:

('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...))))))) ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))

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

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

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

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

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

from pprint import pprint # We can directly call the method # pprint() using itcoordinates = [ { “name”: “Location 1”, “gps”: (29.008966, 111.573724) }, { “name”: “Location 2”, “gps”: (40.1632626, 44.2935926) }, { “name”: “Location 3”, “gps”: (29.476705, 121.869339) }]pprint(coordinates)

输出:

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

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

​英文原文:

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

【责任编辑:赵宁宁 TEL:(010)68476606】


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK