Hướng dẫn how do i turn on pretty print in python? - làm cách nào để bật tính năng in đẹp trong python?

Mã nguồn: lib/pprint.py Lib/pprint.py


Mô-đun pprint cung cấp khả năng cho các cấu trúc dữ liệu Python tùy ý in Pretty Print In Nếu các cấu trúc được định dạng bao gồm các đối tượng không phải là loại Python cơ bản, thì biểu diễn có thể không được tải. Đây có thể là trường hợp nếu các đối tượng như tệp, ổ cắm hoặc lớp được bao gồm, cũng như nhiều đối tượng khác không thể đại diện như các chữ Python.

Biểu diễn được định dạng giữ các đối tượng trên một dòng nếu nó có thể, và chia chúng trên nhiều dòng nếu chúng không phù hợp với chiều rộng được phép. Xây dựng các đối tượng PrettyPrinter một cách rõ ràng nếu bạn cần điều chỉnh ràng buộc chiều rộng.

Từ điển được sắp xếp bằng khóa trước khi màn hình được tính toán.

Mô -đun pprint xác định một lớp:

Lớp ________ 11 ________ 12 (thụt lề = 1, chiều rộng = 80, độ sâu = không, stream = none, *, compact = false, sort_dicts = true(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

Xây dựng một ví dụ PrettyPrinter. Trình xây dựng này hiểu một số tham số từ khóa.

Luồng (mặc định

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']
4) là một đối tượng giống như tệp mà đầu ra sẽ được viết bằng cách gọi phương thức
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']
5 của nó.file-like object to which the output will be written by calling its
>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']
5 method.

Các giá trị khác định cấu hình cách làm tổ của các cấu trúc dữ liệu phức tạp được hiển thị.

thụt lề (mặc định 1) chỉ định lượng thụt thêm thêm cho mỗi cấp độ lồng.

Độ sâu kiểm soát số lượng các mức làm tổ có thể được in; Nếu cấu trúc dữ liệu được in quá sâu, mức chứa tiếp theo được thay thế bằng

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']
6. Theo mặc định, không có ràng buộc về độ sâu của các đối tượng được định dạng.

Chiều rộng (mặc định 80) chỉ định số lượng ký tự tối đa mong muốn trên mỗi dòng trong đầu ra. Nếu một cấu trúc không thể được định dạng trong ràng buộc chiều rộng, một nỗ lực tốt nhất sẽ được thực hiện.

Compact tác động đến cách các chuỗi dài (danh sách, bộ dữ liệu, bộ, v.v.) được định dạng. Nếu compact là sai (mặc định) thì mỗi mục của một chuỗi sẽ được định dạng trên một dòng riêng biệt. Nếu nhỏ gọn là đúng, nhiều mục sẽ phù hợp với chiều rộng sẽ được định dạng trên mỗi dòng đầu ra.

Nếu sort_dict là đúng (mặc định), từ điển sẽ được định dạng với các khóa được sắp xếp, nếu không chúng sẽ hiển thị theo thứ tự chèn.

Nếu UnderCore_Numbers là đúng, các số nguyên sẽ được định dạng với ký tự

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']
7 cho dấu phân cách hàng ngàn, nếu không thì dấu gạch dưới không được hiển thị (mặc định).

Đã thay đổi trong phiên bản 3.4: Đã thêm tham số nhỏ gọn.Added the compact parameter.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số sort_dicts.Added the sort_dicts parameter.

Đã thay đổi trong phiên bản 3.10: Đã thêm tham số UnderCore_Numbers.Added the underscore_numbers parameter.

>>> 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', (...)))))))

Mô -đun pprint cũng cung cấp một số chức năng phím tắt:

________ 11 ________ 20 (đối tượng, thụt lề = 1, chiều rộng = 80, độ sâu = không, *, compact = false, sort_dicts = true(object, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

Trả về biểu diễn được định dạng của đối tượng dưới dạng chuỗi. thụt lề, chiều rộng, độ sâu, nhỏ gọn, sort_dicts và underscore_numbers sẽ được chuyển cho hàm tạo PrettyPrinter làm tham số định dạng.

Đã thay đổi trong phiên bản 3.4: Đã thêm tham số nhỏ gọn.Added the compact parameter.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số sort_dicts.Added the sort_dicts parameter.

Đã thay đổi trong phiên bản 3.10: Đã thêm tham số UnderCore_Numbers.Added the underscore_numbers parameter.

Mô -đun pprint cũng cung cấp một số chức năng phím tắt:(object, *args, sort_dicts=False, **kwargs)

________ 11 ________ 20 (đối tượng, thụt lề = 1, chiều rộng = 80, độ sâu = không, *, compact = false, sort_dicts = true

Trả về biểu diễn được định dạng của đối tượng dưới dạng chuỗi. thụt lề, chiều rộng, độ sâu, nhỏ gọn, sort_dicts và underscore_numbers sẽ được chuyển cho hàm tạo PrettyPrinter làm tham số định dạng.

________ 11 ________ 23 (đối tượng, *args, sort_dicts = false, ** kwargs) ¶(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

In các biểu diễn được định dạng của đối tượng theo sau là một dòng mới. Nếu sort_dicts là sai (mặc định), từ điển sẽ được hiển thị với các khóa của chúng theo thứ tự chèn, nếu không các phím Dict sẽ được sắp xếp. Args và kwargs sẽ được chuyển sang

>>> pprint.isreadable(stuff)
False
4 dưới dạng các tham số định dạng.

Đã thay đổi trong phiên bản 3.4: Đã thêm tham số nhỏ gọn.Added the compact parameter.

Đã thay đổi trong phiên bản 3.8: Đã thêm tham số sort_dicts.Added the sort_dicts parameter.

Đã thay đổi trong phiên bản 3.10: Đã thêm tham số UnderCore_Numbers.Added the underscore_numbers parameter.

>>> import pprint
>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> stuff.insert(0, stuff)
>>> pprint.pprint(stuff)
[,
 'spam',
 'eggs',
 'lumberjack',
 'knights',
 'ni']

Mô -đun pprint cũng cung cấp một số chức năng phím tắt:(object)

________ 11 ________ 20 (đối tượng, thụt lề = 1, chiều rộng = 80, độ sâu = không, *, compact = false, sort_dicts = true

>>> pprint.isreadable(stuff)
False

Trả về biểu diễn được định dạng của đối tượng dưới dạng chuỗi. thụt lề, chiều rộng, độ sâu, nhỏ gọn, sort_dicts và underscore_numbers sẽ được chuyển cho hàm tạo PrettyPrinter làm tham số định dạng.(object)

________ 11 ________ 23 (đối tượng, *args, sort_dicts = false, ** kwargs) ¶

In các biểu diễn được định dạng của đối tượng theo sau là một dòng mới. Nếu sort_dicts là sai (mặc định), từ điển sẽ được hiển thị với các khóa của chúng theo thứ tự chèn, nếu không các phím Dict sẽ được sắp xếp. Args và kwargs sẽ được chuyển sang

>>> pprint.isreadable(stuff)
False
4 dưới dạng các tham số định dạng.

________ 11 ________ 39 (đối tượng) ¶(object)

Trả về một biểu diễn chuỗi của đối tượng, được bảo vệ chống lại các cấu trúc dữ liệu đệ quy. Nếu biểu diễn của đối tượng phơi bày một mục nhập đệ quy, tham chiếu đệ quy sẽ được biểu diễn là

>>> import json
>>> import pprint
>>> from urllib.request import urlopen
>>> with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
...     project_info = json.load(resp)['info']
0. Đại diện không được định dạng.

>>> pprint.saferepr(stuff)
"[, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"

Đẹp đối tượng

PrettyPrinter Các trường hợp có các phương pháp sau:

________ 42 ________ 20 (đối tượng) ¶(object)

Trả về biểu diễn được định dạng của đối tượng. Điều này có tính đến các tùy chọn được chuyển cho hàm tạo PrettyPrinter.

________ 42 ________ 26 (đối tượng) ¶(object)

In biểu diễn được định dạng của đối tượng trên luồng được cấu hình, theo sau là một dòng mới.

Các phương pháp sau đây cung cấp các triển khai cho các chức năng tương ứng của cùng tên. Sử dụng các phương pháp này trên một thể hiện hiệu quả hơn một chút vì các đối tượng ____99 mới không cần phải được tạo.

________ 42 ________ 33 (đối tượng) ¶(object)

Xác định xem biểu diễn được định dạng của đối tượng có thể đọc được hay không, có thể sử dụng hoặc có thể được sử dụng để xây dựng lại giá trị bằng cách sử dụng

>>> pprint.saferepr(stuff)
"[, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
4. Lưu ý rằng điều này trả về
>>> pprint.saferepr(stuff)
"[, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
5 cho các đối tượng đệ quy. Nếu tham số độ sâu của PrettyPrinter được đặt và đối tượng sâu hơn được phép, điều này sẽ trả về
>>> pprint.saferepr(stuff)
"[, 'spam', 'eggs', 'lumberjack', 'knights', 'ni']"
5.

________ 42 ________ 37 (đối tượng) ¶(object)

Xác định xem đối tượng yêu cầu biểu diễn đệ quy.

Phương pháp này được cung cấp dưới dạng móc để cho phép các lớp con sửa đổi cách chuyển đổi các đối tượng thành chuỗi. Việc triển khai mặc định sử dụng các phần bên trong của triển khai

>>> pprint.pprint(project_info)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': ['Development Status :: 3 - Alpha',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: MIT License',
                 'Programming Language :: Python :: 2',
                 'Programming Language :: Python :: 2.6',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3',
                 'Programming Language :: Python :: 3.2',
                 'Programming Language :: Python :: 3.3',
                 'Programming Language :: Python :: 3.4',
                 'Topic :: Software Development :: Build Tools'],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {'Download': 'UNKNOWN',
                  'Homepage': 'https://github.com/pypa/sampleproject'},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
6.

________ 42 ________ 58 (đối tượng, bối cảnh, maxlevels, cấp độ) ¶(object, context, maxlevels, level)

Trả về ba giá trị: Phiên bản được định dạng của đối tượng dưới dạng chuỗi, một cờ cho biết liệu kết quả có thể đọc được hay không và một lá cờ cho biết liệu đệ quy có được phát hiện hay không. Đối số đầu tiên là đối tượng được trình bày. Thứ hai là một từ điển chứa

>>> pprint.pprint(project_info)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': ['Development Status :: 3 - Alpha',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: MIT License',
                 'Programming Language :: Python :: 2',
                 'Programming Language :: Python :: 2.6',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3',
                 'Programming Language :: Python :: 3.2',
                 'Programming Language :: Python :: 3.3',
                 'Programming Language :: Python :: 3.4',
                 'Topic :: Software Development :: Build Tools'],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {'Download': 'UNKNOWN',
                  'Homepage': 'https://github.com/pypa/sampleproject'},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
9 của các đối tượng là một phần của bối cảnh trình bày hiện tại (các thùng chứa trực tiếp và gián tiếp cho đối tượng đang ảnh hưởng đến bản trình bày) như các khóa; Nếu một đối tượng cần được trình bày đã được biểu diễn trong ngữ cảnh, giá trị trả về thứ ba phải là
>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
0. Các cuộc gọi đệ quy đến phương thức
>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
1 sẽ thêm các mục bổ sung cho các thùng chứa vào từ điển này. Đối số thứ ba, MaxLevels, đưa ra giới hạn được yêu cầu để đệ quy; Đây sẽ là
>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}
2 nếu không có giới hạn được yêu cầu. Đối số này nên được thông qua không sửa đổi cho các cuộc gọi đệ quy. Đối số thứ tư, cấp độ, đưa ra cấp độ hiện tại; Các cuộc gọi đệ quy phải được truyền một giá trị ít hơn so với cuộc gọi hiện tại.

Thí dụ¶

Để chứng minh một số cách sử dụng của hàm

>>> pprint.isreadable(stuff)
False
4 và các tham số của nó, hãy để tìm nạp thông tin về một dự án từ PYPI:

>>> import json
>>> import pprint
>>> from urllib.request import urlopen
>>> with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
...     project_info = json.load(resp)['info']

Ở dạng cơ bản của nó,

>>> pprint.isreadable(stuff)
False
4 hiển thị toàn bộ đối tượng:

>>> pprint.pprint(project_info)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': ['Development Status :: 3 - Alpha',
                 'Intended Audience :: Developers',
                 'License :: OSI Approved :: MIT License',
                 'Programming Language :: Python :: 2',
                 'Programming Language :: Python :: 2.6',
                 'Programming Language :: Python :: 2.7',
                 'Programming Language :: Python :: 3',
                 'Programming Language :: Python :: 3.2',
                 'Programming Language :: Python :: 3.3',
                 'Programming Language :: Python :: 3.4',
                 'Topic :: Software Development :: Build Tools'],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {'Download': 'UNKNOWN',
                  'Homepage': 'https://github.com/pypa/sampleproject'},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

Kết quả có thể được giới hạn ở một độ sâu nhất định (Ellipsis được sử dụng cho nội dung sâu hơn):

>>> pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the project.\n'
                '\n'
                'The file should use UTF-8 encoding and be written using '
                'ReStructured Text. It\n'
                'will be used to generate the project webpage on PyPI, and '
                'should be written for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would include an overview of '
                'the project, basic\n'
                'usage examples, etc. Generally, including the project '
                'changelog in here is not\n'
                'a good idea, although a simple "What\'s New" section for the '
                'most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

Ngoài ra, chiều rộng ký tự tối đa có thể được đề xuất. Nếu một đối tượng dài không thể được chia, chiều rộng được chỉ định sẽ bị vượt quá:

>>> pprint.pprint(project_info, depth=1, width=60)
{'author': 'The Python Packaging Authority',
 'author_email': '',
 'bugtrack_url': None,
 'classifiers': [...],
 'description': 'A sample Python project\n'
                '=======================\n'
                '\n'
                'This is the description file for the '
                'project.\n'
                '\n'
                'The file should use UTF-8 encoding and be '
                'written using ReStructured Text. It\n'
                'will be used to generate the project '
                'webpage on PyPI, and should be written '
                'for\n'
                'that purpose.\n'
                '\n'
                'Typical contents for this file would '
                'include an overview of the project, '
                'basic\n'
                'usage examples, etc. Generally, including '
                'the project changelog in here is not\n'
                'a good idea, although a simple "What\'s '
                'New" section for the most recent version\n'
                'may be appropriate.',
 'description_content_type': None,
 'docs_url': None,
 'download_url': 'UNKNOWN',
 'downloads': {...},
 'home_page': 'https://github.com/pypa/sampleproject',
 'keywords': 'sample setuptools development',
 'license': 'MIT',
 'maintainer': None,
 'maintainer_email': None,
 'name': 'sampleproject',
 'package_url': 'https://pypi.org/project/sampleproject/',
 'platform': 'UNKNOWN',
 'project_url': 'https://pypi.org/project/sampleproject/',
 'project_urls': {...},
 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
 'requires_dist': None,
 'requires_python': None,
 'summary': 'A sample Python project',
 'version': '1.2.0'}

Làm thế nào để bạn làm đẹp đầu ra trong Python?

Có một số cách để định dạng đầu ra ...
Để sử dụng các chuỗi chữ được định dạng, hãy bắt đầu một chuỗi với F hoặc F trước dấu ngoặc kép mở hoặc đánh dấu ba trích dẫn ..
Phương thức chuỗi str.format () giúp người dùng có được đầu ra fancier ..

Mô -đun nào được yêu cầu trong in ấn đẹp?

Mô-đun PPRINT cung cấp khả năng cho các cấu trúc dữ liệu Python tùy ý in Pretty in provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter.

Pprint có được xây dựng không?

Bài viết này là về một mô-đun tích hợp khá hữu ích trong Python, Pprint.Mô-đun PPRINT cung cấp khả năng cho các cấu trúc dữ liệu Python độc đoán của bản in khá đẹp theo một cách được định dạng tốt và dễ đọc hơn!built-in module in Python, pprint. The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a well-formatted and more readable way!

Làm cách nào để in từ pprint?

Đánh dấu văn bản bạn muốn in trên trang và nhấn Ctrl + P trên PC hoặc lệnh + P trên máy tính Apple để mở các tùy chọn in..