python之pprint模块简用

pprint 模块提供了一种格式化输出Python基础类型数据的方法,使得输出既可解析又易读。当数据对象不是Python基础类型时,可能无法正确加载。PrettyPrinter类允许自定义缩进、宽度和深度等参数,以适应不同场景。示例展示了pprint函数及参数的使用,当结构无法在限制宽度内格式化时,会尽力尝试。
摘要由CSDN通过智能技术生成

pprint module提供了可以按照某个格式正确的显示python已知类型数据的一种方法,这种格式可被解析器解析, 又很易读。但是,如果已知格式的数据对象不是python的基础类型,这种表示方法就有可能加载失败。这种情况一般是对象为 files, sockets, classes, or instances are included, as well as many other built-in objects which are not representable as Python constants。

该方法输出将对象的输出分隔成单行显示,并在宽度设置不适合时,将其分成多行显示。

class pprint.PrettyPrinter(indent=1,width=80,depth=None,stream=None)

pprint. pformat ( object, indent=1, width=80, depth=None )仅仅想获得数据而不是输出数据也可以用pformat

Return the formatted representation of object as a string. indent,widthanddepth will be passed to thePrettyPrinter constructor as formatting parameters.

pprint. pprint ( object, stream=None, indent=1, width=80, depth=None )

Prints the formatted representation of object on stream, followed by anewline.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值