Python pprint and pythonpprint

Source: Internet
Author: User
Tags pprint

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.