您好,欢迎访问一九零五行业门户网

Python中的pprint折腾记

1.背景
看到这里提到了pprint。 
打算去试试.
2.pprint简介
找到在线官网解释:
pprint — data pretty printer 
就是一个,方便大家打印一些,相对复杂的变量的好东西。
3.使用pprint
去写点代码试试。
代码:
代码如下:
#-------------------------------------------------------------------------------
# name:        【记录】折腾python中的pprint
# author:      crifan li
#
# created:     06/01/2013
# copyright:   (c) crifan li 2013
#-------------------------------------------------------------------------------
import pprint;
import re;
def pprintdemo():
    varslist = [
        [1, 2, 3],
        [ab, c, def],
        re.compile(\w+),
        (123, abc),
        {
            key1:value1,
            key2:value2,
        },
    ];
    for value in varslist:
        print value;
    print -*80;
    pp = pprint.prettyprinter(indent=4);
    for value in varslist:
        pp.pprint(value);
    print =*80;
    stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'];
    stuff.insert(0, stuff[:]);
    print stuff;
    print -*80;
    pp.pprint(stuff)
if __name__ == '__main__':
    pprintdemo();
效果:
代码如下:
[1, 2, 3]
['ab', 'c', 'def']
('123', 'abc')
{'key2': 'value2', 'key1': 'value1'}
--------------------------------------------------------------------------------
[1, 2, 3]
['ab', 'c', 'def']
('123', 'abc')
{   'key1': 'value1', 'key2': 'value2'}
================================================================================
[['spam', 'eggs', 'lumberjack', 'knights', 'ni'], 'spam', 'eggs', 'lumberjack', 'knights', 'ni']
--------------------------------------------------------------------------------
[   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
    'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']
4.总结
pprint,有点意思。
以后可以用在代码调试过程中。
其它类似信息

推荐信息