python之pprint模块简用
来源:互联网 发布:智能网络电力仪表 编辑:程序博客网 时间:2024/06/05 23:00
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.
Prints the formatted representation of object on stream, followed by anewline.
- pprint.isreadable(object)
Determine if the formatted representation of object is “readable,” or can be used to reconstruct the value usingeval(). This always returnsFalse for recursive objects.
>>> import sys>>> import pprint>>> pprint.pprint(sys.path)['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']>>> print sys.path #一行显示,显示很长['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']>>> string = pprint.pformat(sys.path)>>> string #还是一行"['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']">>> print string #输出格式变成了单行['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']>>>
>>> stuff = ['spam','eggs','lumberjack','knight','ni']>>> pprint.pprint(stuff)['spam', 'eggs', 'lumberjack', 'knight', 'ni']>>> print stuff['spam', 'eggs', 'lumberjack', 'knight', 'ni']>>> stuff.insert(0,stuff)>>> pprint.pprint(stuff)#If stream is None, sys.stdout is used. This may be used in the interactive interpreter instead of a print statement# for inspecting values.[<Recursion on list with id=33573024>, 'spam', 'eggs', 'lumberjack', 'knight', 'ni']>>> print stuff[[...], 'spam', 'eggs', 'lumberjack', 'knight', 'ni']>>>
>>> pprint.isreadable(stuff)#Determine if the formatted representation of object is “readable“False>>> pprint.isreadable(sys.path)True>>>
# 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.
>>> tup = ('spam',('eggs',('lumberjack',)))>>> stuff = ['a'*5, tup, ['b'*5, 'c'*5],['c'*5, 'd'*5]]>>> stuff['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff)['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,depth=3)['aaaaa', ('spam', ('eggs', (...,))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,depth=4)['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,depth=2)['aaaaa', ('spam', (...)), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,width=40)['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,width=80)['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>> pprint.pprint(stuff,width=3)['aaaaa', ('spam', ('eggs', ('lumberjack',))), ['bbbbb', 'ccccc'], ['ccccc', 'ddddd']]>>>
总的来说,感觉目前用处不是很大,没有什么特别的地方,就是对输出的一种格式化。
- python之pprint模块简用
- python之pprint模块简用
- python之pprint模块
- python之pprint模块的pprint方法
- Python 模块学习 -----pprint
- python pprint模块
- python pprint模块
- python pprint模块
- Python pprint模块
- python pprint模块
- Python学习笔记 --- pprint模块
- python数据格式化之pprint
- python数据格式化之pprint
- python数据格式化之pprint
- 利用 python pprint模块 优雅的输出
- [Python模块学习]使用pprint模块格式化打印
- Python中的pprint
- Python开发--pprint
- Delphi FireMonkey 学习笔记 – TPopup 控件的使用
- 变量的命名法
- 华为2014年机试题1
- 一切皆为 JavaScript
- 共同语言
- python之pprint模块简用
- SVN无法读取cruuent修复方法 Can't read file : End of file found 文件:txn_current、current
- Windows快捷键大全
- jsp 中文乱码问题
- 便利的开发工具 CppUnit 快速使用指南
- 搭建mysql负载均衡及高可用环境
- 波多尔地区pm10浓度的预测
- gridview选择行。
- 找工作