Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to print Python objects

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

本篇内容主要讲解"怎么打印Python对象",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"怎么打印Python对象"吧!

pprint 是"pretty printer"的简写,"pretty"的含义是"漂亮的、美观的",还有表示"相当地"的程度语气,因此它的含义便是:(相当)美观的打印。

这是个相当简单却有用的模块,主要用于打印复杂的数据结构对象,例如多层嵌套的列表、元组和字典等。

先看看 print() 打印的一个例子:

mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]print(mylist)# 结果如下:['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']

这是一个简单的例子,全部打印在一行里。

想象一下,如果对象中的元素是多层嵌套的内容(例如复杂的 Json 数据),或者有超多的元素(例如在列表中存了很多 URL 链接),再打印出来会是怎样?

那肯定是一团糟的,不好阅读!

使用 pprint 模块的 pprint() 替代 print(),可以解决如下痛点:

设置合适的行宽度,作适当的换行

设置打印的缩进、层级,进行格式化打印

判断对象中是否有无限循环,并优化打印内容

1、简单使用

语法:pprint(object, stream=None, indent=1, width=80, depth=None, *,compact=False)

默认的行宽度参数为 80,当打印的字符(character)小于 80 时,pprint() 基本上等同于内置函数 print(),当字符超出时,它会作美化,进行格式化输出:

import pprint# 打印上例的 mylistpprint.pprint(mylist)# 打印的元素是换行的(因为超出80字符):['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']

2、设置缩进为 4 个空格(默认为1)

pprint.pprint(mylist, indent=4)[ 'Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']

3、设置打印的行宽

mydict = {'students': [{'name':'Tom', 'age': 18},{'name':'Jerry', 'age': 19}]}pprint.pprint(mydict)# 未超长:{'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]}pprint.pprint(mydict, width=20)# 超长1:{'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]}pprint.pprint(mydict, width=70)# 超长2:{'students': [{'age': 18, 'name': 'Tom'}, {'age': 19, 'name': 'Jerry'}]}

4、设置打印的层级(默认全打印)

newlist = [1, [2, [3, [4, [5]]]]]pprint.pprint(newlist, depth=3)# 超出的层级会用...表示[1, [2, [3, [...]]]]

5、优化循环结构的打印

当列表或其它数据结构中出现循环引用时,要完整打印出所有内容是不可能的。

所以 print 作了简化处理,就像上例一样,只打印外层的壳,而不打印内层循环的东西。

这种处理方式是简化了,但没有指出是谁导致了循环,还容易看漏。

pprint() 方法作了改进,遇到无限循环结构时,会表示成 的格式。

还有个 saferepr() 方法,也是这样优化,而且返回的是个字符串:

newlist = [1, 2]newlist.insert(0, newlist)# 列表元素指向列表自身,造成循环引用# 直接 print 的结果是:[[...], 1, 2]pprint.pprint(newlist)# [, 1, 2]pprint.saferepr(newlist)# '[, 1, 2]'

6、判断是否出现循环结构

有两个方法判断一个对象中是否出现无限循环:

pprint.isrecursive(newlist)# Truepprint.isreadable(newlist)# False

isreadable() 除了能像 isrecursive() 一样判断循环,还能判断该格式化内容是否可被 eval() 重构。

以上就是 pprint 模块的快捷入门介绍,除此之外,还有 pformat() 方法、PrettyPrinter 类,以及某些参数的使用等内容,我觉得没有大用,就不多说了。

最后,还有个小小的点:

用 pprint() 替换 print() 的技巧

在不考虑 print() 函数本身的参数的情况下,可以在引入 pprint 模块后,写上 "print = pprint.pprint",令 print() 起到改头换面的效果:

import pprintprint = pprint.pprintmylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]print(mylist)# 可对比本文开头的例子['Beautiful is better than ugly.', 'Explicit is better than implicit.', 'Simple is better than complex.', 'Complex is better than complicated.']到此,相信大家对"怎么打印Python对象"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 274

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report