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

pprint python_让我们来谈谈python中的prettyprint和pprint当你开始学习 python 编程的时候 你做的第一件事是什么 相信我们都已经通过 HelloWorld 程序开始了我们的 python 之旅 在 python 中 它可以在一行中完成 print HelloWorld 但是 在使用 print 函数打印字典 列表或任何其他复杂数据类型时 您是否遇到过这种痛苦呢 由于不适当的缩进问题 我们经常在 python 嵌套数据结构的输出中遇到可读性方面的困难 让我们

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

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

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

print(“Hello World”)

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

让我们在这里试试代码:

coordinates= [

{

“name”: “Location 1”,

“gps”: (29.008966, 111.)

},

{

“name”: “Location 2”,

“gps”: (40., 44.)

},

{

“name”: “Location 3”,

“gps”: (29., 121.)

}

]

print(coordinates)

以上代码的输出:

[{‘gps’: (29.008966, 111.), ‘name’:

‘Location 1’}, {‘gps’: (40., 44.),

‘name’: ‘Location 2’}, {‘gps’: (29., 121.),

‘name’: ‘Location 3’}]

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

如果解决这个问题呢?

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

pprint python_让我们来谈谈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.)

},

{

“name”: “Location 2”,

“gps”: (40., 44.)

},

{

“name”: “Location 3”,

“gps”: (29., 121.)

}

]

pprint(coordinates)

输出:

[{‘gps’: (29.008966, 111.), ‘name’: ‘Location 1’},

{‘gps’: (40., 44.), ‘name’: ‘Location 2’},

{‘gps’: (29., 121.), ‘name’: ‘Location 3’}]

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

​英文原文:

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

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

点赞 0

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/223329.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午2:19
下一篇 2026年3月17日 下午2:20


相关推荐

  • 解决docker端口映射无法访问问题的方法_为什么有的网页无法访问

    解决docker端口映射无法访问问题的方法_为什么有的网页无法访问https://cloud.tencent.com/developer/article/1768097https://blog.csdn.net/li_101357/article/details/78415461

    2022年10月17日
    5
  • 设置标题栏文字颜色。

    设置标题栏文字颜色。米度软件 www midosoft cn nbsp nbsp 通过系统 API 函数 SetSysColor 可以设置应用程序标题栏文字的颜色 该函数声明如下 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp BOOLSetSysCo nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp intcElements nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp

    2026年3月26日
    2
  • centos 删除软链接(centos软路由)

    添加软连接软连接,为某一个文件在另外一个位置建立一个同步的连接在此处创建软连接后,访问到此处的软连接,就会定位到软连接指向的位置。相当于把一个文件夹放到了多个位置,但其实还是只有一份,并不是复制。具体用法是:ln-s源文件目标文件会针对源文件创建一个软连接(目标文件),链接到源文件。别搞反了。前面的源文件,是需要在当前位置能访问到的文件。后面的是目标文件,是新创建出来的链接。例…

    2022年4月14日
    256
  • 非常好的opengl 教程[通俗易懂]

    非常好的opengl教程, stepbystep的从最简单的三角形开始一直复杂的3d场景绘制http://ogldev.atspace.co.uk/index.html

    2022年4月17日
    59
  • fastjson 注解(JAVA注解)

    查看@JSONField注解的源码可以了解到它的作用范围是在方法(METHOD)、属性(FIELD)、方法中的参数(PARAMETER)上。1.作用在FIELD(成员变量上)注意:1、若属性是私有的,必须有set*方法。否则无法反序列化。packagecom.zhujie;importcom.alibaba.fastjson.JSONObject;importcom…

    2022年4月10日
    769
  • Wireshark 无法找到接口解决方法

    Wireshark 无法找到接口解决方法解决方法 下载安装 WinPcap 如果已安装 卸载后重新安装重启 wireshark 看是否能找到本地接口如果仍然提示无法找到接口 关闭 wireshark 将 WinPcap 卸载 安装 npcap 重启 wireshark 会找到本地接口

    2026年3月16日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号