博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python pprint模块
阅读量:4171 次
发布时间:2019-05-26

本文共 8231 字,大约阅读时间需要 27 分钟。

简介

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

模块方法:

1.class pprint.PrettyPrinter(indent=1,width=80,depth=Nonestream=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=80depth=None) 

   返回格式化的对象字符串

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

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

4.pprint.isreadable(object) 

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

5.pprint.isrecursive(object) 

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

   eg: 

>>> import pprint>>> l=[2,3,4]>>> pprint.isrecursive(l)False>>> l.append(l)>>> print l[2, 3, 4, [...]]>>> pprint.isrecursive(l)True>>>

6.pprint.saferepr(object) 

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

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

[python]   
  1. import pprint  
  2.   
  3. data = (  
  4.     "this is a string", [1234], ("more tuples",  
  5.     1.02.34.5), "this is yet another string"  
  6.     )  
  7.   
  8. pprint.pprint(data)  

输出:

('this is a string',

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

原文链接:

 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,width and depth will be passed to the  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 a newline.

pprint.
isreadable
(
object
)

Determine if the formatted representation of object is “readable,” or can be used to reconstruct the value using . This always returns False for recursive objects.

[python]   
  1. >>> import sys  
  2. >>> import pprint  
  3. >>> pprint.pprint(sys.path)  
  4. ['D:/Python/tmp',  
  5.  'D:\\Python\\tmp',  
  6.  'C:\\Python27\\Lib\\idlelib',  
  7.  'C:\\Python27\\lib\\site-packages\\zope.interface-4.0.0-py2.7-win32.egg',  
  8.  'C:\\Python27\\lib\\site-packages\\pyopenssl-0.13-py2.7-win32.egg',  
  9.  'C:\\Python27\\lib\\site-packages\\scrapy-0.14.4-py2.7.egg',  
  10.  'C:\\Python27\\lib\\site-packages\\w3lib-1.2-py2.7.egg',  
  11.  'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.3-py2.7-win32.egg',  
  12.  'C:\\Python27\\lib\\site-packages\\paramiko-1.9.0-py2.7.egg',  
  13.  'C:\\Python27\\lib\\site-packages\\pycrypto-2.6-py2.7-win32.egg',  
  14.  'C:\\Python27\\lib\\site-packages\\requests-2.0.0-py2.7.egg',  
  15.  'C:\\Python27\\lib\\site-packages\\reportlab-2.7-py2.7-win32.egg',  
  16.  'C:\\Windows\\system32\\python27.zip',  
  17.  'C:\\Python27\\DLLs',  
  18.  'C:\\Python27\\lib',  
  19.  'C:\\Python27\\lib\\plat-win',  
  20.  'C:\\Python27\\lib\\lib-tk',  
  21.  'C:\\Python27',  
  22.  'C:\\Python27\\lib\\site-packages',  
  23.  'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']  
  24. >>> print sys.path #一行显示,显示很长  
  25. ['D:/Python/tmp''D:\\Python\\tmp''C:\\Python27\\Lib\\idlelib''C:\\Python27\\lib\\site-packages\\zope.interface-4.0.0-py2.7-win32.egg''C:\\Python27\\lib\\site-packages\\pyopenssl-0.13-py2.7-win32.egg''C:\\Python27\\lib\\site-packages\\scrapy-0.14.4-py2.7.egg''C:\\Python27\\lib\\site-packages\\w3lib-1.2-py2.7.egg''C:\\Python27\\lib\\site-packages\\mysql_python-1.2.3-py2.7-win32.egg''C:\\Python27\\lib\\site-packages\\paramiko-1.9.0-py2.7.egg''C:\\Python27\\lib\\site-packages\\pycrypto-2.6-py2.7-win32.egg''C:\\Python27\\lib\\site-packages\\requests-2.0.0-py2.7.egg''C:\\Python27\\lib\\site-packages\\reportlab-2.7-py2.7-win32.egg''C:\\Windows\\system32\\python27.zip''C:\\Python27\\DLLs''C:\\Python27\\lib''C:\\Python27\\lib\\plat-win''C:\\Python27\\lib\\lib-tk''C:\\Python27''C:\\Python27\\lib\\site-packages''C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']  
  26. >>> string = pprint.pformat(sys.path)  
  27. >>> string #还是一行  
  28. "['D:/Python/tmp',\n 'D:\\\\Python\\\\tmp',\n 'C:\\\\Python27\\\\Lib\\\\idlelib',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\zope.interface-4.0.0-py2.7-win32.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\pyopenssl-0.13-py2.7-win32.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\scrapy-0.14.4-py2.7.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\w3lib-1.2-py2.7.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\mysql_python-1.2.3-py2.7-win32.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\paramiko-1.9.0-py2.7.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\pycrypto-2.6-py2.7-win32.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\requests-2.0.0-py2.7.egg',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\reportlab-2.7-py2.7-win32.egg',\n 'C:\\\\Windows\\\\system32\\\\python27.zip',\n 'C:\\\\Python27\\\\DLLs',\n 'C:\\\\Python27\\\\lib',\n 'C:\\\\Python27\\\\lib\\\\plat-win',\n 'C:\\\\Python27\\\\lib\\\\lib-tk',\n 'C:\\\\Python27',\n 'C:\\\\Python27\\\\lib\\\\site-packages',\n 'C:\\\\Python27\\\\lib\\\\site-packages\\\\wx-2.8-msw-unicode']"  
  29. >>> print string #输出格式变成了单行  
  30. ['D:/Python/tmp',  
  31.  'D:\\Python\\tmp',  
  32.  'C:\\Python27\\Lib\\idlelib',  
  33.  'C:\\Python27\\lib\\site-packages\\zope.interface-4.0.0-py2.7-win32.egg',  
  34.  'C:\\Python27\\lib\\site-packages\\pyopenssl-0.13-py2.7-win32.egg',  
  35.  'C:\\Python27\\lib\\site-packages\\scrapy-0.14.4-py2.7.egg',  
  36.  'C:\\Python27\\lib\\site-packages\\w3lib-1.2-py2.7.egg',  
  37.  'C:\\Python27\\lib\\site-packages\\mysql_python-1.2.3-py2.7-win32.egg',  
  38.  'C:\\Python27\\lib\\site-packages\\paramiko-1.9.0-py2.7.egg',  
  39.  'C:\\Python27\\lib\\site-packages\\pycrypto-2.6-py2.7-win32.egg',  
  40.  'C:\\Python27\\lib\\site-packages\\requests-2.0.0-py2.7.egg',  
  41.  'C:\\Python27\\lib\\site-packages\\reportlab-2.7-py2.7-win32.egg',  
  42.  'C:\\Windows\\system32\\python27.zip',  
  43.  'C:\\Python27\\DLLs',  
  44.  'C:\\Python27\\lib',  
  45.  'C:\\Python27\\lib\\plat-win',  
  46.  'C:\\Python27\\lib\\lib-tk',  
  47.  'C:\\Python27',  
  48.  'C:\\Python27\\lib\\site-packages',  
  49.  'C:\\Python27\\lib\\site-packages\\wx-2.8-msw-unicode']  
  50. >>>   

[python]   
  1. >>> stuff = ['spam','eggs','lumberjack','knight','ni']  
  2. >>> pprint.pprint(stuff)  
  3. ['spam''eggs''lumberjack''knight''ni']  
  4. >>> print stuff  
  5. ['spam''eggs''lumberjack''knight''ni']  
  6. >>> stuff.insert(0,stuff)  
  7. >>> pprint.pprint(stuff)#If stream is None, sys.stdout is used.  This may be used in the interactive interpreter instead of a print statement  
  8. # for inspecting values.  
  9. [<Recursion on list with id=33573024>,  
  10.  'spam',  
  11.  'eggs',  
  12.  'lumberjack',  
  13.  'knight',  
  14.  'ni']  
  15. >>> print stuff  
  16. [[...], 'spam''eggs''lumberjack''knight''ni']  
  17. >>>   
[python]   
  1. >>> pprint.isreadable(stuff)#Determine if the formatted representation of object is “readable“  
  2. False  
  3. >>> pprint.isreadable(sys.path)  
  4. True  
  5. >>>  

# This example demonstrates several uses of the pprint() function and its parameters.

如下例,依次为:

#list中包含tuple
#按照指定深度来显示stuff内容
#The number of levels which may be printed is controlled by depth; if the data structure being printed is too deep, the next contained level is replaced by .... 
#The desired output width is constrained using the width parameter; the default is 80 characters.

If a structure cannot be formatted within the constrained width, a best effort will be made.

[python]   
  1. >>> tup = ('spam',('eggs',('lumberjack',)))  
  2. >>> stuff = ['a'*5, tup, ['b'*5'c'*5],['c'*5'd'*5]]  
  3. >>> stuff  
  4. ['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb''ccccc'], ['ccccc''ddddd']]  
  5. >>> pprint.pprint(stuff)  
  6. ['aaaaa',  
  7.  ('spam', ('eggs', ('lumberjack',))),  
  8.  ['bbbbb''ccccc'],  
  9.  ['ccccc''ddddd']]  
  10. >>> pprint.pprint(stuff,depth=3)  
  11. ['aaaaa', ('spam', ('eggs', (...,))), ['bbbbb''ccccc'], ['ccccc''ddddd']]  
  12. >>> pprint.pprint(stuff,depth=4)  
  13. ['aaaaa',  
  14.  ('spam', ('eggs', ('lumberjack',))),  
  15.  ['bbbbb''ccccc'],  
  16.  ['ccccc''ddddd']]  
  17. >>> pprint.pprint(stuff,depth=2)  
  18. ['aaaaa', ('spam', (...)), ['bbbbb''ccccc'], ['ccccc''ddddd']]  
  19. >>> pprint.pprint(stuff,width=40)  
  20. ['aaaaa',  
  21.  ('spam', ('eggs', ('lumberjack',))),  
  22.  ['bbbbb''ccccc'],  
  23.  ['ccccc''ddddd']]  
  24. >>> pprint.pprint(stuff,width=80)  
  25. ['aaaaa',  
  26.  ('spam', ('eggs', ('lumberjack',))),  
  27.  ['bbbbb''ccccc'],  
  28.  ['ccccc''ddddd']]  
  29. >>> pprint.pprint(stuff,width=3)  
  30. ['aaaaa',  
  31.  ('spam',  
  32.   ('eggs',  
  33.    ('lumberjack',))),  
  34.  ['bbbbb',  
  35.   'ccccc'],  
  36.  ['ccccc',  
  37.   'ddddd']]  
  38. >>>   

原文链接:

转载地址:http://xnyai.baihongyu.com/

你可能感兴趣的文章
PostgreSQL数据库管理 第二章体系结构
查看>>
PostgreSQL数据库管理 第三章实例管理与管理工具
查看>>
PostgreSQL数据库管理第七章流复制
查看>>
PostgreSQL数据库管理第十章Repmgr
查看>>
PostgreSQL数据库管理 第八章日常运维
查看>>
MySQL数据库管理-体系结构
查看>>
软考UML
查看>>
信息系统的生命周期各阶段及说明
查看>>
Ubuntulinux离线安装ClamTk杀毒软件步骤和使用方法
查看>>
摆脱贫穷2021V1
查看>>
Android.Could not find *.apk
查看>>
JNI
查看>>
Android基于TranslateAnimation的动画动态菜单
查看>>
android NDK中的GCC编译器
查看>>
Android NOtification 使用
查看>>
Android的SharedPreferences保存与删除数据简单实例
查看>>
android 如何从sqlite读取数据到spinner下拉中显示
查看>>
Android实现开机自动运行程序
查看>>
最近几天搭建MySql且连接问题总结
查看>>
搭建Tomcat
查看>>