温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python中prettyprint和pprint的作用是什么

发布时间:2021-07-05 17:56:59 来源:亿速云 阅读:263 作者:Leah 栏目:编程语言

python中prettyprint和pprint的作用是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

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

python中prettyprint和pprint的作用是什么

pprint是什么?

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

pprint与print有何不同?

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

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

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

那么,让我们深入pprint…

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

import pprint

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

pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847]) # Instantiating pprint object my_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 PrettyPrinter pp = pprint.PrettyPrinter(depth=6) # default configuration  # of depthbeing none is changed to depth = 6 # Now it will print till depth of six brackets pp.pprint(tuple1) #Using only pprint() object pprint.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 it 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)  } ] 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中prettyprint和pprint的作用是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI