This article mainly introduces pprint tossing notes in Python. This article focuses on the use of pprint and provides examples. For more information, see
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:
The 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:
The 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.