假设我们用 print() 打印一些复杂的嵌套数据, 一旦我们开始处理多层嵌套的数据结构,print() 就变得一团糟。使用 pprint() 替代 print()pprint 模块是 Python 标准库的一部分,这意味着我们不需要安装任何东西,只需导入即可。在 pprint 模块中,有一个名为 pprint 的函数(是的,它们同名)。pprint 是 "pretty-...
假设我们用print()打印一些复杂的嵌套数据, 一旦我们开始处理多层嵌套的数据结构,print()就变得一团糟。 使用pprint()替代print() pprint模块是 Python 标准库的一部分,这意味着我们不需要安装任何东西,只需导入即可。 在pprint模块中,有一个名为pprint的函数(是的,它们同名)。pprint是 "pretty-print" 的缩写,意思...
现在,我们可以使用.pprint()对象或实例化我们自己的pprint对象PrettyPrinter()。 代码语言:javascript 代码运行次数:0 运行 AI代码解释 pprint.pprint(['Radha',1,'Hari','Simesh',25,847])# Instantiating pprint object my_pprint=pprint.PrettyPrinter()my_pprint.pprint(['Radha',1,'Hari','Simesh',25,84...
('parrot', ('fresh fruit',)))# Using PrettyPrinterpp=pprint.PrettyPrinter(depth=6) # default configuration# of depthbeing none is changed todepth=6# Now it will print till depth of six bracketspp.pprint(tuple1)#Using only pprint() objectpprint.pprint(pprint.pprint(tuple1,depth=6))pprint...
pprint对字典 列表这类数据结构确实很好用,但遇到类、字符串仍然和 print 差别不大,因此这里有一个国人写的第三方库介绍给大家。 beeprint 除了和 pprint 一样支持 dict, list, tuple 等常规变量的格式化输出,还支持 object 变量打印、长文本自动剪切。
A pretty printer shows the structure of an object. If the argument is a collection, then it displays the structure of each element. This module has some similarities with pprint; however this is a new implementation. This module also shows the field values for an argument object; if the ...
pprint.pprint(object,stream=None,indent=1,width=80,depth=None,*,compact=False) stream:输出流,默认是 sys.stdout ,也就是屏幕输出。 indent:缩进空格数。 width:每行最大宽度,默认为80个字符,超过宽度会换行,但如果单个对象超过不会被换行,比如一段长字符串。
print(data) print("---分界线---") pprint.pprint(data) 代码运行结果: 本例使用了数据结构较为复杂的较长数据,可见pprint()输出的更加规范易读。 扩展阅读: pprint模块还有其它的一些方法如下所示,详见官方文档: pprint.pformat((object,indent=1,width=80, depth=None) #返回格式化的对象字符串 pprint....
It’s possible to create an instance ofPrettyPrinterthat has defaults you’ve defined. Once you have this new instance of your customPrettyPrinterobject, you can use it by calling the.pprint()method on thePrettyPrinterinstance: Python
1. Python Pretty Print JSON String We can use thedumps()method to get the pretty formatted JSON string. importjson json_data='[{"ID":10,"Name":"Pankaj","Role":"CEO"},'\'{"ID":20,"Name":"David Lee","Role":"Editor"}]'json_object=json.loads(json_data)json_formatted_str=json....