Python pprint and pythonpprint
1. Background
Pprint is mentioned here.
I'm going to try it.
2. Introduction to pprint
Find the explanation on the official online website:
Pprint-Data pretty printer
It is a good thing for you to easily print some relatively complex variables.
3. Use pprint
Try writing some code.
Code:
Copy codeThe Code is as follows:
#-------------------------------------------------------------------------------
# Name: [Record] pprint in Python
# 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', 'egg', 'lumberjack', 'knights ', 'ni'];
Stuff. insert (0, stuff [:]);
Print stuff;
Print "-" * 80;
Pp. pprint (stuff)
If _ name _ = '_ main __':
PprintDemo ();
Effect:
Copy codeThe Code is as follows:
[1, 2, 3]
['AB', 'C', 'def ']
<_ Sre. SRE_Pattern object at 0x00000000030DD378>
('20140901', 'abc ')
{'Key2': 'value2', 'key1': 'value1 '}
--------------------------------------------------------------------------------
[1, 2, 3]
['AB', 'C', 'def ']
<_ Sre. SRE_Pattern object at 0x00000000030DD378>
('20140901', 'abc ')
{'Key1': 'value1', 'key2': 'value2 '}
========================================================== ==========================================================
[['Spam', 'egg', 'lumberjack', 'knights ', 'ni'], 'spam', 'eggs', 'lumberjack', 'knights ', 'ni']
--------------------------------------------------------------------------------
[['Spam', 'egg', 'lumberjack', 'knights ', 'ni'],
'Spam ',
'Eggs ',
'Lumberjack ',
'Knights ',
'Ni']
4. Summary
Pprint.
It can be used in code debugging later.