Introduce a very easy-to-use Python module - pprint module. I believe you will love it

Posted by spode on Thu, 11 Nov 2021 23:16:43 +0100

1, pprint beautiful print data structure

The pprint module contains a "beautiful printer" to generate a beautiful view of the data structure. The formatting tool will generate some representations of the data structure, which can not only be correctly parsed by the interpreter, but also be easy for people to read. The output will be placed on one line as much as possible and indented when decomposed into multiple lines.

1. Printing

from pprint import pprint

data = [
    (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H',
         'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),
    (3, ['m', 'n']),
    (4, ['o', 'p', 'q']),
    (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),
]
print('PRINT:')
print(data)
print()
print('PPRINT:')
pprint(data)

pprint() formats an object and passes it as a parameter into a data stream (or the default sys.stdout).

PRINT:
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}), (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H', 'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}), (3, ['m', 'n']), (4, ['o', 'p', 'q']), (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]

PPRINT:
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
 (2,
  {'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H',
   'i': 'I',
   'j': 'J',
   'k': 'K',
   'l': 'L'}),
 (3, ['m', 'n']),
 (4, ['o', 'p', 'q']),
 (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]

2. Formatting

To format a data structure instead of writing it directly to a stream (that is, for logging), you can use pformat() to build a string representation.

import logging
from pprint import pformat

data = [
    (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H',
         'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),
    (3, ['m', 'n']),
    (4, ['o', 'p', 'q']),
    (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),
]
logging.basicConfig(
    level=logging.DEBUG,
    format='%(levelname)-8s %(message)s',
)
logging.debug('Logging pformatted data')
formatted = pformat(data)
for line in formatted.splitlines():
    logging.debug(line.rstrip())

The formatted string can then be printed separately or logged.

DEBUG    Logging pformatted data
DEBUG    [(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
DEBUG     (2,
DEBUG      {'e': 'E',
DEBUG       'f': 'F',
DEBUG       'g': 'G',
DEBUG       'h': 'H',
DEBUG       'i': 'I',
DEBUG       'j': 'J',
DEBUG       'k': 'K',
DEBUG       'l': 'L'}),
DEBUG     (3, ['m', 'n']),
DEBUG     (4, ['o', 'p', 'q']),
DEBUG     (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]

3. Any class

If a custom class defines a__ repr__ () method, the PrettyPrinter class used by pprint() can also handle such custom classes.

from pprint import pprint


class node:
    def __init__(self, name, contents=[]):
        self.name = name
        self.contents = contents[:]

    def __repr__(self):
        return (
                'node(' + repr(self.name) + ', ' +
                repr(self.contents) + ')'
        )


trees = [
    node('node-1'),
    node('node-2', [node('node-2-1')]),
    node('node-3', [node('node-3-1')]),
]
pprint(trees)

Returns the full string representation using the representation of nested objects combined by PrettyPrinter.

[node('node-1', []),
 node('node-2', [node('node-2-1', [])]),
 node('node-3', [node('node-3-1', [])])]

4. Recursion

The recursive data structure is represented by a reference to the original data source in the form of < recurrence on typename with id = number >

'''
No one answers the problems encountered in learning? Xiaobian created a Python Learning exchange group: 725638078
 Look for like-minded friends to help each other,There are also good videos and tutorials in the group PDF e-book!
'''
from pprint import pprint

local_data = ['a', 'b', 1, 2]
local_data.append(local_data)
print('id(local_data) =>', id(local_data))
pprint(local_data)

In this example, the list local_data is added to itself, which creates a recursive reference.

id(local_data) => 2763816527488
['a', 'b', 1, 2, <Recursion on list with id=2763816527488>]

5. Limit nested output

For very deep data structures, you may not be required to include all the details in the output. The data may not be properly formatted, the formatted text may be too large to manage, or some data may be redundant.

from pprint import pprint

data = [
    (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H',
         'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),
    (3, ['m', 'n']),
    (4, ['o', 'p', 'q']),
    (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),
]
pprint(data, depth=1)
pprint(data, depth=2)

Using the depth parameter, you can control the depth of recursive processing of nested data structures. Levels not included in the output are indicated by ellipsis.

[(...), (...), (...), (...), (...)]
[(1, {...}), (2, {...}), (3, [...]), (4, [...]), (5, [...])]

6. Control output width

The default output width for formatted text is 80 columns. To adjust this width, you can use the parameter width in pprint().

from pprint import pprint

data = [
    (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H',
         'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),
    (3, ['m', 'n']),
    (4, ['o', 'p', 'q']),
    (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),
]
for width in [80, 5]:
    print('WIDTH =', width)
    pprint(data, width=width)
    print()

When the width is too small to meet the formatted data structure, if truncation or conversion will lead to illegal syntax, it will not be truncated or converted.

WIDTH = 80
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
 (2,
  {'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H',
   'i': 'I',
   'j': 'J',
   'k': 'K',
   'l': 'L'}),
 (3, ['m', 'n']),
 (4, ['o', 'p', 'q']),
 (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]

WIDTH = 5
[(1,
  {'a': 'A',
   'b': 'B',
   'c': 'C',
   'd': 'D'}),
 (2,
  {'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H',
   'i': 'I',
   'j': 'J',
   'k': 'K',
   'l': 'L'}),
 (3,
  ['m',
   'n']),
 (4,
  ['o',
   'p',
   'q']),
 (5,
  ['r',
   's',
   'tu',
   'v',
   'x',
   'y',
   'z'])]

The compact flag tells pprint() to try to put more data on each row instead of breaking up a complex data structure into multiple rows.

from pprint import pprint

data = [
    (1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
    (2, {'e': 'E', 'f': 'F', 'g': 'G', 'h': 'H',
         'i': 'I', 'j': 'J', 'k': 'K', 'l': 'L'}),
    (3, ['m', 'n']),
    (4, ['o', 'p', 'q']),
    (5, ['r', 's', 't''u', 'v', 'x', 'y', 'z']),
]
for width in [80, 5]:
    print('WIDTH =', width)
    pprint(data, width=width)
    print()

This example shows that when a data structure cannot be placed on another row, it will be decomposed (the same is true for the second item in the data list). If multiple elements can be placed on a row (such as the third and fourth members), they are placed on the same row.

WIDTH = 80
[(1, {'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D'}),
 (2,
  {'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H',
   'i': 'I',
   'j': 'J',
   'k': 'K',
   'l': 'L'}),
 (3, ['m', 'n']),
 (4, ['o', 'p', 'q']),
 (5, ['r', 's', 'tu', 'v', 'x', 'y', 'z'])]

WIDTH = 5
[(1,
  {'a': 'A',
   'b': 'B',
   'c': 'C',
   'd': 'D'}),
 (2,
  {'e': 'E',
   'f': 'F',
   'g': 'G',
   'h': 'H',
   'i': 'I',
   'j': 'J',
   'k': 'K',
   'l': 'L'}),
 (3,
  ['m',
   'n']),
 (4,
  ['o',
   'p',
   'q']),
 (5,
  ['r',
   's',
   'tu',
   'v',
   'x',
   'y',
   'z'])]

At the end, I recommend a very good learning tutorial for you. I hope it will be helpful for you to learn Python!

Python basics tutorial recommendation

Python crawler case tutorial recommendation