手机
当前位置:查字典教程网 >脚本专栏 >python >Python中的pprint折腾记
Python中的pprint折腾记
摘要:1.背景看到这里提到了pprint。打算去试试.2.pprint简介找到在线官网解释:pprint—Dataprettyprinter就是一...

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']

<_sre.SRE_Pattern object at 0x00000000030DD378>

('123', 'abc')

{'key2': 'value2', 'key1': 'value1'}

--------------------------------------------------------------------------------

[1, 2, 3]

['ab', 'c', 'def']

<_sre.SRE_Pattern object at 0x00000000030DD378>

('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,有点意思。

以后可以用在代码调试过程中。

【Python中的pprint折腾记】相关文章:

Python中的jquery PyQuery库使用小结

Python下的Mysql模块MySQLdb安装详解

详细介绍Python语言中的按位运算符

Python学习资料

Python中使用中文的方法

Python写的PHPMyAdmin暴力破解工具代码

python中文乱码的解决方法

详解Python中的__init__和__new__

Python代码的打包与发布详解

python中的sort方法使用详解

精品推荐
分类导航