zoukankan      html  css  js  c++  java
  • pprint模块介绍

    简介
    pprint模块 提供了打印出任何Python数据结构类和方法。

    模块方法:

    1.class pprint.PrettyPrinter(indent=1,width=80,depth=None, stream=None)    
       创建一个PrettyPrinter对象

        indent --- 缩进,width --- 一行最大宽度,

        depth --- 打印的深度,这个主要是针对一些可递归的对象,如果超出指定depth,其余的用"..."代替。

                     eg: a=[1,2,[3,4,],5]  a的深度就是2; b=[1,2,[3,4,[5,6]],7,8] b的深度就是3

        stream ---指输出流对象,如果stream=None,那么输出流对象默认是sys.stdout

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

       返回格式化的对象字符串

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

      输出格式的对象字符串到指定的stream,最后以换行符结束。

    4.pprint.isreadable(object) 

       判断对象object的字符串对象是否可读

    5.pprint.isrecursive(object) 

       判断对象是否需要递归的表示

       eg: pprint.isrecursive(a)  --->False

            pprint.isrecursive([1,2,3])-->True

    6.pprint.saferepr(object) 

       返回一个对象字符串,对象中的子对象如果是可递归的,都被替换成<Recursionontypename withid=number>.这种形式。


    PrettyPrinter 对象具有的方法与上面类似,不在赘述。

    # Author:Sunshine
    
    import pprint
    
    data = (
        "this is a string", [1, 2, 3, 4], ("more tuples",1.0, 2.3, 4.5), "this is yet another string"
    )
    
    pprint.pprint(data)
    View Code

    以下是输出:

    ('this is a string',
    [1, 2, 3, 4],
    ('more tuples', 1.0, 2.3, 4.5),
    'this is yet another string')

  • 相关阅读:
    Kubernetes 用户流量接入方案
    给Nginx配置日志格式和调整日期格式
    唇亡齿寒,运维与安全
    Nginx记录用户请求Header到access log
    Kubernetes中利用Kubectl set 让Deployment更新镜像
    故障管理:故障定级和定责
    使用 Elastic 技术栈构建 Kubernetes全栈监控
    故障管理:鼓励做事,而不是处罚错误
    故障管理:谈谈我对故障的理解
    稳定性实践:开关和预案
  • 原文地址:https://www.cnblogs.com/ZWSunshine/p/7469964.html
Copyright © 2011-2022 走看看