• [python] pprint模块


    简介
    pprint 包含一个“美观打印机”,用于生成数据结构的一个美观视图。
    格式化工具会生成数据结构的一些表示,不仅可以由解释器正确地解析,而且便于阅读。输出尽可能放在一行上,分解为多行时则需要缩进。

    模块方法

    1.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

    6.pprint.saferepr(object) 

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

    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)
    
    #out
    #('this is a string',
    #[1, 2, 3, 4],
    #('more tuples', 1.0, 2.3, 4.5),
    #'this is yet another string')
    

      

  • 相关阅读:
    Volume 6. Mathematical Concepts and Methods
    git帮助网址
    ubuntu 下安装wine
    ubuntu 通过ppa源安装mysql5.6
    ubuntu qq安装
    ubuntu14.04 fcitx安装
    language support图标消失
    ubuntu root用户登陆
    ubuntu 安装codeblocks13.12
    ubuntu tomcat自启动
  • 原文地址:https://www.cnblogs.com/P3nguin/p/7684136.html
Copyright © 2020-2023  润新知