python之pprint模块
来源:互联网 发布:淘宝客用什么软件下载 编辑:程序博客网 时间:2024/05/18 02:35
学过perl同学都知道perl有Data::Dumper,在python有相同功能模块pprint。接下来会把学习python文档做些笔记,以便以后查阅。pprint能够打印python的数据结构,看起来更直观。
PrettyPrinter类有三个参数:indent, depth, width
indent:展示数据时,缩进多少(每个递归层)
depth:最多显示层级
width:展示一行宽度,默认80
对每层递归都缩进10
>>> import pprint>>> stuff = ['a'*10, 'b'*10, 'c'*10, 'd'*10, 'e'*10]>>> stuff.insert(0, stuff[:])>>> pp = pprint.PrettyPrinter(indent=10);>>> pp.pprint(stuff)[ [ 'aaaaaaaaaa', 'bbbbbbbbbb', 'cccccccccc', 'dddddddddd', 'eeeeeeeeee'], 'aaaaaaaaaa', 'bbbbbbbbbb', 'cccccccccc', 'dddddddddd', 'eeeeeeeeee']
一共是6层,如果只显示5层,那python就会用”…”来展示为未显示列表
>>> tup = ('a', ('b', ('c', ('d', ('e', ('f',))))))>>> pp = pprint.PrettyPrinter(depth=6)>>> pp.pprint(tup)('a', ('b', ('c', ('d', ('e', ('f',))))))>>> pp = pprint.PrettyPrinter(depth=5)>>> pp.pprint(tup)('a', ('b', ('c', ('d', ('e', (...,))))))
一共是92个字符,所以width=93时,就可在一行内放下
>>> stuff = ['a'*5, 'b'*5, 'c'*5, 'd'*5, 'e'*5]>>> stuff.insert(0, stuff[:])>>> pp = pprint.PrettyPrinter();>>> pp.pprint(stuff)[['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee'], 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']>>> pp = pprint.PrettyPrinter(width=93);>>> pp.pprint(stuff);[['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee'], 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']
PrettyPrinter的函数:pformat、pprint、isreadable、isrecursive、saferepr
具体参考(http://docs.python.org/library/pprint.html)
pformat以字符串返回
>>> stuff = ['a'*5, 'b'*5, 'c'*5, 'd'*5, 'e'*5]>>> pp = pprint.PrettyPrinter();>>> pp.pformat(stuff);"['aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']"
Print the formatted representation of object on the configured stream(不知道怎么翻译好)
>>> stuff = ['a'*5, 'b'*5, 'c'*5, 'd'*5, 'e'*5]>>> stuff.insert(0, stuff)>>> pprint.pprint(stuff)[<Recursion on list with id=29923736>, 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']
isreadable来确认对象是否可读,如果是递归对象(多层),就会返回false
>>> stuff = ['a'*5, 'b'*5, 'c'*5, 'd'*5, 'e'*5]>>> pp = pprint.PrettyPrinter();>>> pp.isreadable(stuff)True>>> stuff.insert(0, stuff) >>> pp.isreadable(stuff)False
isrecursive判断是否是递归对象
>>> stuff = ['a'*5, 'b'*5, 'c'*5, 'd'*5, 'e'*5] >>> pp = pprint.PrettyPrinter();>>> pp.isrecursive(stuff)False>>> stuff.insert(0, stuff)>>> pp.isrecursive(stuff)True
对递归对象进行保护,递归对象就会显示
>>> pprint.saferepr(stuff)"[<Recursion on list with id=29924312>, 'aaaaa', 'bbbbb', 'ccccc', 'ddddd', 'eeeee']"
- 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模块格式化打印
- Python中的pprint
- Python开发--pprint
- C#与excel互操作的错误无法将类型为“Microsoft.Office.Interop.Excel.ApplicationClass”的 COM 对象强制
- 组合数
- allwinner script_parser_fetch()函数
- TI BQ27210電池監測IC
- jdbc ,ibatis,hibernate三者的区别
- python之pprint模块
- NYOJ.524.A-B Problem
- MTK 平台上查询当前使用的摄像头模组及所支持预览分辨率
- MiniGUI通用窗口操作函数
- 链接错误-库冲突(libcmt.lib和libcmtd.lib)
- c# 数据库的写入到excel
- http://blog.csdn.net/chaijunkun/article/details/7208828
- 让vim显示空格,及tab字符 vim 多行注释
- 第四周项目2