zoukankan      html  css  js  c++  java
  • pprint of Python

    pprint

    对于非基本的数据类型, 即对象或者成员中含有对象的数据,print仅仅打印其对象基本信息,在一行中。不会打印数据内容。

    如果想打印数据,则需要使用pprint模块。

    此模块包括打印接口, 和打印类。

    打印类用于定义打印格式, 提供复用。

    https://docs.python.org/3.5/library/pprint.html

    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.

    Debugging data structures can be time consuming, especially when wading through printed output of large sequences or dictionaries. Use pprint to create easy-to-read representations that can be printed to the console or written to a log file for easier debugging.

    DEMO

    >>> 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': 'glyph@twistedmatrix.com',
              '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
    '
                             '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'}]}
    

    pprint.PrettyPrinter

    >>> import pprint
    >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
    >>> stuff.insert(0, stuff[:])
    >>> pp = pprint.PrettyPrinter(indent=4)
    >>> pp.pprint(stuff)
    [   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
        'spam',
        'eggs',
        'lumberjack',
        'knights',
        'ni']
    >>> pp = pprint.PrettyPrinter(width=41, compact=True)
    >>> pp.pprint(stuff)
    [['spam', 'eggs', 'lumberjack',
      'knights', 'ni'],
     'spam', 'eggs', 'lumberjack', 'knights',
     'ni']
    >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
    ... ('parrot', ('fresh fruit',))))))))
    >>> pp = pprint.PrettyPrinter(depth=6)
    >>> pp.pprint(tup)
    ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))
  • 相关阅读:
    QT项目之创建.pri文件
    QT中使用pri子工程
    foreach
    Qt--解析Json
    C++构造函数的default和delete
    QVariantMap 和 QVariant
    __attribute__中constructor和destructor
    唯品会
    获取图片和下载到本地和名字和链接的获取
    python实现屏幕截图
  • 原文地址:https://www.cnblogs.com/lightsong/p/13939683.html
Copyright © 2011-2022 走看看