GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.python.org — 8.11. pprint — Data pretty printer — Python 3.4.10 documentation
This document is for an old version of Python that is no longer supported.
You should upgrade, and read the
Python documentation for the current stable release.
Navigation8.11. pprint — Data pretty printer¶Source code: Lib/pprint.py The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter. If the formatted structures include objects which are not fundamental Python types, the representation may not be loadable. This may be the case if objects such as files, sockets or classes are included, as well as many other objects which are not representable as Python literals. The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width. Construct PrettyPrinter objects explicitly if you need to adjust the width constraint. Dictionaries are sorted by key before the display is computed. The pprint module defines one class:
The pprint module also provides several shortcut functions:
One more support function is also defined:
8.11.1. PrettyPrinter Objects¶PrettyPrinter instances have the following methods:
The following methods provide the implementations for the corresponding functions of the same names. Using these methods on an instance is slightly more efficient since new PrettyPrinter objects don’t need to be created.
This method is provided as a hook to allow subclasses to modify the way objects are converted to strings. The default implementation uses the internals of the saferepr() implementation.
8.11.2. Example¶To demonstrate several uses of the pprint() function and its parameters, let’s fetch information about a project from PyPI: >>> import json >>> import pprint >>> from urllib.request import urlopen >>> with urlopen('http://pypi.python.org/pypi/Twisted/json') as url: ... http_info = url.info() ... raw_data = url.read().decode(http_info.get_content_charset()) >>> project_info = json.loads(raw_data) In its basic form, pprint() shows the whole object: >>> pprint.pprint(project_info) {'info': {'_pypi_hidden': False, '_pypi_ordering': 125, 'author': 'Glyph Lefkowitz', 'author_email': '[email protected]', 'bugtrack_url': '', 'cheesecake_code_kwalitee_id': None, 'cheesecake_documentation_id': None, 'cheesecake_installability_id': None, 'classifiers': ['Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 2 :: Only'], 'description': 'An extensible framework for Python programming, ' 'with special focus\r\n' 'on event-based network programming and ' 'multiprotocol integration.', 'docs_url': '', 'download_url': 'UNKNOWN', 'home_page': 'http://twistedmatrix.com/', 'keywords': '', 'license': 'MIT', 'maintainer': '', 'maintainer_email': '', 'name': 'Twisted', 'package_url': 'http://pypi.python.org/pypi/Twisted', 'platform': 'UNKNOWN', 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0', 'requires_python': None, 'stable_version': None, 'summary': 'An asynchronous networking framework written in Python', 'version': '12.3.0'}, 'urls': [{'comment_text': '', 'downloads': 71844, 'filename': 'Twisted-12.3.0.tar.bz2', 'has_sig': False, 'md5_digest': '6e289825f3bf5591cfd670874cc0862d', 'packagetype': 'sdist', 'python_version': 'source', 'size': 2615733, 'upload_time': '2012-12-26T12:47:03', 'url': 'https://pypi.python.org/packages/source/T/Twisted/Twisted-12.3.0.tar.bz2'}, {'comment_text': '', 'downloads': 5224, 'filename': 'Twisted-12.3.0.win32-py2.7.msi', 'has_sig': False, 'md5_digest': '6b778f5201b622a5519a2aca1a2fe512', 'packagetype': 'bdist_msi', 'python_version': '2.7', 'size': 2916352, 'upload_time': '2012-12-26T12:48:15', 'url': 'https://pypi.python.org/packages/2.7/T/Twisted/Twisted-12.3.0.win32-py2.7.msi'}]} The result can be limited to a certain depth (ellipsis is used for deeper contents): >>> pprint.pprint(project_info, depth=2) {'info': {'_pypi_hidden': False, '_pypi_ordering': 125, 'author': 'Glyph Lefkowitz', 'author_email': '[email protected]', 'bugtrack_url': '', 'cheesecake_code_kwalitee_id': None, 'cheesecake_documentation_id': None, 'cheesecake_installability_id': None, 'classifiers': [...], 'description': 'An extensible framework for Python programming, ' 'with special focus\r\n' 'on event-based network programming and ' 'multiprotocol integration.', 'docs_url': '', 'download_url': 'UNKNOWN', 'home_page': 'http://twistedmatrix.com/', 'keywords': '', 'license': 'MIT', 'maintainer': '', 'maintainer_email': '', 'name': 'Twisted', 'package_url': 'http://pypi.python.org/pypi/Twisted', 'platform': 'UNKNOWN', 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0', 'requires_python': None, 'stable_version': None, 'summary': 'An asynchronous networking framework written in Python', 'version': '12.3.0'}, 'urls': [{...}, {...}]} Additionally, maximum character width can be suggested. If a long object cannot be split, the specified width will be exceeded: >>> pprint.pprint(project_info, depth=2, width=50) {'info': {'_pypi_hidden': False, '_pypi_ordering': 125, 'author': 'Glyph Lefkowitz', 'author_email': '[email protected]', 'bugtrack_url': '', 'cheesecake_code_kwalitee_id': None, 'cheesecake_documentation_id': None, 'cheesecake_installability_id': None, 'classifiers': [...], 'description': 'An extensible ' 'framework for ' 'Python programming, ' 'with special ' 'focus\r\n' 'on event-based ' 'network programming ' 'and multiprotocol ' 'integration.', 'docs_url': '', 'download_url': 'UNKNOWN', 'home_page': 'http://twistedmatrix.com/', 'keywords': '', 'license': 'MIT', 'maintainer': '', 'maintainer_email': '', 'name': 'Twisted', 'package_url': 'http://pypi.python.org/pypi/Twisted', 'platform': 'UNKNOWN', 'release_url': 'http://pypi.python.org/pypi/Twisted/12.3.0', 'requires_python': None, 'stable_version': None, 'summary': 'An asynchronous ' 'networking framework ' 'written in Python', 'version': '12.3.0'}, 'urls': [{...}, {...}]} Table Of ContentsPrevious topic8.10. copy — Shallow and deep copy operations Next topic8.12. reprlib — Alternate repr() implementation This PageQuick searchEnter search terms or a module, class or function name. Navigation
© Copyright 1990-2019, Python Software Foundation.
The Python Software Foundation is a non-profit corporation. Please donate. Last updated on Jun 16, 2019. Found a bug? Created using Sphinx 1.2.3. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |