日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)Python教程:pprint—-數(shù)據(jù)美化輸出

pprint —- 數(shù)據(jù)美化輸出

源代碼: Lib/pprint.py

目前成都創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、盤(pán)錦網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。


pprint 模塊提供了“美化打印”任意 python 數(shù)據(jù)結(jié)構(gòu)的功能,這種美化形式可用作對(duì)解釋器的輸入。 如果經(jīng)格式化的結(jié)構(gòu)包含非基本 Python 類(lèi)型的對(duì)象,則其美化形式可能無(wú)法被加載。 包含文件、套接字或類(lèi)對(duì)象,以及許多其他不能用 Python 字面值來(lái)表示的對(duì)象都有可能導(dǎo)致這樣的結(jié)果。

格式化后的形式會(huì)在可能的情況下以單行來(lái)表示對(duì)象,并在無(wú)法在允許寬度內(nèi)容納對(duì)象的情況下將其分為多行。 如果你需要調(diào)整寬度限制則應(yīng)顯式地構(gòu)造 PrettyPrinter 對(duì)象。

字典在計(jì)算其顯示形式前會(huì)先根據(jù)鍵來(lái)排序。

在 3.9 版更改: 添加了對(duì)美化打印 types.SimpleNamespace 的支持。

在 3.10 版更改: 添加了對(duì)美化打印 dataclasses.dataclass 的支持。

pprint 模塊定義了一個(gè)類(lèi):

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, **, compact=False, sort_dicts=True, underscore_numbers=False*)

構(gòu)造一個(gè) PrettyPrinter 實(shí)例。 這個(gè)構(gòu)造器支持一些關(guān)鍵字形參。

stream (default sys.stdout) is a file-like object to which the output will be written by calling its write() method. If both stream and sys.stdout are None, then pprint() silently returns.

其他值可用來(lái)配置復(fù)雜數(shù)據(jù)結(jié)構(gòu)嵌套要以何種形式被展示。

indent (默認(rèn)為 1) 指定要為每個(gè)縮進(jìn)層級(jí)添加的縮進(jìn)量。

depth 控制可被打印的縮進(jìn)層級(jí)數(shù)量;如果要打印的數(shù)據(jù)結(jié)構(gòu)層級(jí)過(guò)深,則其所包含的下一層級(jí)將用 ... 替換。 默認(rèn)情況下,對(duì)于被格式化對(duì)象的層級(jí)深度沒(méi)有任何限制。

width (默認(rèn)為 80) 指定輸出中每行所允許的最大字符數(shù)。 如果一個(gè)數(shù)據(jù)結(jié)構(gòu)無(wú)法在寬度限制之內(nèi)被格式化,將顯示盡可能多的內(nèi)容。

compact 影響長(zhǎng)序列(列表、元組、集合等等)的格式化方式。 如果 compact 為假值(默認(rèn))則序列的每一項(xiàng)將格式化為單獨(dú)的行。 如果 compact 為真值,則每個(gè)輸出行格式化時(shí)將在 width 的限制之內(nèi)盡可能地容納多個(gè)條目。

如果 sort_dicts 為真值(默認(rèn)),字典在格式化時(shí)將基于鍵進(jìn)行排序,否則它們將按插入順序顯示。

如果 underscore_numbers 為真值,整數(shù)在格式化時(shí)將使用 _ 字符作為千位分隔符,否則不顯示下劃線(默認(rèn))。

在 3.4 版更改: 增加了 compact 形參。

在 3.8 版更改: 增加了 sort_dicts 形參。

在 3.10 版更改: 添加了 underscore_numbers 形參。

在 3.11 版更改: No longer attempts to write to sys.stdout if it is None.

 
 
 
 
  1. >>> import pprint
  2. >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
  3. >>> stuff.insert(0, stuff[:])
  4. >>> pp = pprint.PrettyPrinter(indent=4)
  5. >>> pp.pprint(stuff)
  6. [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
  7. 'spam',
  8. 'eggs',
  9. 'lumberjack',
  10. 'knights',
  11. 'ni']
  12. >>> pp = pprint.PrettyPrinter(width=41, compact=True)
  13. >>> pp.pprint(stuff)
  14. [['spam', 'eggs', 'lumberjack',
  15. 'knights', 'ni'],
  16. 'spam', 'eggs', 'lumberjack', 'knights',
  17. 'ni']
  18. >>> tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead',
  19. ... ('parrot', ('fresh fruit',))))))))
  20. >>> pp = pprint.PrettyPrinter(depth=6)
  21. >>> pp.pprint(tup)
  22. ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', (...)))))))

pprint.pformat(object, indent=1, width=80, depth=None, **, compact=False, sort_dicts=True, underscore_numbers=False*)

Return the formatted representation of object as a string. indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the PrettyPrinter constructor as formatting parameters and their meanings are as described in its documentation above.

pprint.pp(object, \args, sort_dicts=False, **kwargs*)

打印 object 的格式化表示并附帶一個(gè)換行符。 如果 sort_dicts 為假值(默認(rèn)),字典將按鍵的插入順序顯示,否則將按字典鍵排序。 argskwargs 將作為格式化形參被傳給 pprint()。

3.8 新版功能.

pprint.pprint(object, stream=None, indent=1, width=80, depth=None, **, compact=False, sort_dicts=True, underscore_numbers=False*)

Prints the formatted representation of object on stream, followed by a newline. If stream is None, sys.stdout is used. This may be used in the interactive interpreter instead of the print() function for inspecting values (you can even reassign print = pprint.pprint for use within a scope).

The configuration parameters stream, indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the PrettyPrinter constructor and their meanings are as described in its documentation above.

 
 
 
 
  1. >>> import pprint
  2. >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
  3. >>> stuff.insert(0, stuff)
  4. >>> pprint.pprint(stuff)
  5. [,
  6. 'spam',
  7. 'eggs',
  8. 'lumberjack',
  9. 'knights',
  10. 'ni']

pprint.isreadable(object)

確定 object 的格式化表示是否“可讀”,或是否可被用來(lái)通過(guò) eval() 重新構(gòu)建對(duì)象的值。 此函數(shù)對(duì)于遞歸對(duì)象總是返回 False

 
 
 
 
  1. >>> pprint.isreadable(stuff)
  2. False

pprint.isrecursive(object)

確定 object 是否需要遞歸表示。

此外還定義了一個(gè)支持函數(shù):

pprint.saferepr(object)

返回 object 的字符串表示,并為遞歸數(shù)據(jù)結(jié)構(gòu)提供保護(hù)。 如果 object 的表示形式公開(kāi)了一個(gè)遞歸條目,該遞歸引用會(huì)被表示為 。 該表示因而不會(huì)進(jìn)行其它格式化。

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

PrettyPrinter 對(duì)象

PrettyPrinter 的實(shí)例具有下列方法:

PrettyPrinter.pformat(object)

返回 object 格式化表示。 這會(huì)將傳給 PrettyPrinter 構(gòu)造器的選項(xiàng)納入考慮。

PrettyPrinter.pprint(object)

在所配置的流上打印 object 的格式化表示,并附加一個(gè)換行符。

下列方法提供了與同名函數(shù)相對(duì)應(yīng)的實(shí)現(xiàn)。 在實(shí)例上使用這些方法效率會(huì)更高一些,因?yàn)椴恍枰獎(jiǎng)?chuàng)建新的 PrettyPrinter 對(duì)象。

PrettyPrinter.isreadable(object)

確定對(duì)象的格式化表示是否“可讀”,或者是否可使用 eval() 重建對(duì)象值。 請(qǐng)注意此方法對(duì)于遞歸對(duì)象將返回 False。 如果設(shè)置了 PrettyPrinter 的 depth 形參并且對(duì)象深度超出允許范圍,此方法將返回 False。

PrettyPrinter.isrecursive(object)

確定對(duì)象是否需要遞歸表示。

此方法作為一個(gè)鉤子提供,允許子類(lèi)修改將對(duì)象轉(zhuǎn)換為字符串的方式。 默認(rèn)實(shí)現(xiàn)使用 saferepr() 實(shí)現(xiàn)的內(nèi)部方式。

PrettyPrinter.format(object, context, maxlevels, level)

返回三個(gè)值:字符串形式的 object 已格式化版本,指明結(jié)果是否可讀的旗標(biāo),以及指明是否檢測(cè)到遞歸的旗標(biāo)。 第一個(gè)參數(shù)是要表示的對(duì)象。 第二個(gè)是以對(duì)象 id() 為鍵的字典,這些對(duì)象是當(dāng)前表示上下文的一部分(影響 object 表示的直接和間接容器);如果需要呈現(xiàn)一個(gè)已經(jīng)在 context 中表示的對(duì)象,則第三個(gè)返回值應(yīng)當(dāng)為 True。 對(duì) format() 方法的遞歸調(diào)用應(yīng)當(dāng)將容器的附加條目添加到此字典中。 第三個(gè)參數(shù) maxlevels 給出了對(duì)遞歸的請(qǐng)求限制;如果沒(méi)有請(qǐng)求限制則其值將為 0。 此參數(shù)應(yīng)當(dāng)不加修改地傳給遞歸調(diào)用。 第四個(gè)參數(shù) level 給出于當(dāng)前層級(jí);傳給遞歸調(diào)用的參數(shù)值應(yīng)當(dāng)小于當(dāng)前調(diào)用的值。

示例

為了演示 pprint() 函數(shù)及其形參的幾種用法,讓我們從 PyPI 獲取關(guān)于某個(gè)項(xiàng)目的信息:

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

pprint() 以其基本形式顯示了整個(gè)對(duì)象:

 
 
 
 
  1. >>> pprint.pprint(project_info)
  2. {'author': 'The Python Packaging Authority',
  3. 'author_email': 'pypa-dev@googlegroups.com',
  4. 'bugtrack_url': None,
  5. 'classifiers': ['Development Status :: 3 - Alpha',
  6. 'Intended Audience :: Developers',
  7. 'License :: OSI Approved :: MIT License',
  8. 'Programming Language :: Python :: 2',
  9. 'Programming Language :: Python :: 2.6',
  10. 'Programming Language :: Python :: 2.7',
  11. 'Programming Language :: Python :: 3',
  12. 'Programming Language :: Python :: 3.2',
  13. 'Programming Language :: Python :: 3.3',
  14. 'Programming Language :: Python :: 3.4',
  15. 'Topic :: Software Development :: Build Tools'],
  16. 'description': 'A sample Python project\n'
  17. '=======================\n'
  18. '\n'
  19. 'This is the description file for the project.\n'
  20. '\n'
  21. 'The file should use UTF-8 encoding and be written using '
  22. 'ReStructured Text. It\n'
  23. 'will be used to generate the project webpage on PyPI, and '
  24. 'should be written for\n'
  25. 'that purpose.\n'
  26. '\n'
  27. 'Typical contents for this file would include an overview of '
  28. 'the project, basic\n'
  29. 'usage examples, etc. Generally, including the project '
  30. 'changelog in here is not\n'
  31. 'a good idea, although a simple "What\'s New" section for the '
  32. 'most recent version\n'
  33. 'may be appropriate.',
  34. 'description_content_type': None,
  35. 'docs_url': None,
  36. 'download_url': 'UNKNOWN',
  37. 'downloads': {'last_day': -1, 'last_month': -1, 'last_week': -1},
  38. 'home_page': 'https://github.com/pypa/sampleproject',
  39. 'keywords': 'sample setuptools development',
  40. 'license': 'MIT',
  41. 'maintainer': None,
  42. 'maintainer_email': None,
  43. 'name': 'sampleproject',
  44. 'package_url': 'https://pypi.org/project/sampleproject/',
  45. 'platform': 'UNKNOWN',
  46. 'project_url': 'https://pypi.org/project/sampleproject/',
  47. 'project_urls': {'Download': 'UNKNOWN',
  48. 'Homepage': 'https://github.com/pypa/sampleproject'},
  49. 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
  50. 'requires_dist': None,
  51. 'requires_python': None,
  52. 'summary': 'A sample Python project',
  53. 'version': '1.2.0'}

結(jié)果可以被限制到特定的 depth (更深層的內(nèi)容將使用省略號(hào)):

 
 
 
 
  1. >>> pprint.pprint(project_info, depth=1)
  2. {'author': 'The Python Packaging Authority',
  3. 'author_email': 'pypa-dev@googlegroups.com',
  4. 'bugtrack_url': None,
  5. 'classifiers': [...],
  6. 'description': 'A sample Python project\n'
  7. '=======================\n'
  8. '\n'
  9. 'This is the description file for the project.\n'
  10. '\n'
  11. 'The file should use UTF-8 encoding and be written using '
  12. 'ReStructured Text. It\n'
  13. 'will be used to generate the project webpage on PyPI, and '
  14. 'should be written for\n'
  15. 'that purpose.\n'
  16. '\n'
  17. 'Typical contents for this file would include an overview of '
  18. 'the project, basic\n'
  19. 'usage examples, etc. Generally, including the project '
  20. 'changelog in here is not\n'
  21. 'a good idea, although a simple "What\'s New" section for the '
  22. 'most recent version\n'
  23. 'may be appropriate.',
  24. 'description_content_type': None,
  25. 'docs_url': None,
  26. 'download_url': 'UNKNOWN',
  27. 'downloads': {...},
  28. 'home_page': 'https://github.com/pypa/sampleproject',
  29. 'keywords': 'sample setuptools development',
  30. 'license': 'MIT',
  31. 'maintainer': None,
  32. 'maintainer_email': None,
  33. 'name': 'sampleproject',
  34. 'package_url': 'https://pypi.org/project/sampleproject/',
  35. 'platform': 'UNKNOWN',
  36. 'project_url': 'https://pypi.org/project/sampleproject/',
  37. 'project_urls': {...},
  38. 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
  39. 'requires_dist': None,
  40. 'requires_python': None,
  41. 'summary': 'A sample Python project',
  42. 'version': '1.2.0'}

此外,還可以設(shè)置建議的最大字符 width。 如果一個(gè)對(duì)象無(wú)法被拆分,則將超出指定寬度:

 
 
 
 
  1. >>> pprint.pprint(project_info, depth=1, width=60)
  2. {'author': 'The Python Packaging Authority',
  3. 'author_email': 'pypa-dev@googlegroups.com',
  4. 'bugtrack_url': None,
  5. 'classifiers': [...],
  6. 'description': 'A sample Python project\n'
  7. '=======================\n'
  8. '\n'
  9. 'This is the description file for the '
  10. 'project.\n'
  11. '\n'
  12. 'The file should use UTF-8 encoding and be '
  13. 'written using ReStructured Text. It\n'
  14. 'will be used to generate the project '
  15. 'webpage on PyPI, and should be written '
  16. 'for\n'
  17. 'that purpose.\n'
  18. '\n'
  19. 'Typical contents for this file would '
  20. 'include an overview of the project, '
  21. 'basic\n'
  22. 'usage examples, etc. Generally, including '
  23. 'the project changelog in here is not\n'
  24. 'a good idea, although a simple "What\'s '
  25. 'New" section for the most recent version\n'
  26. 'may be appropriate.',
  27. 'description_content_type': None,
  28. 'docs_url': None,
  29. 'download_url': 'UNKNOWN',
  30. 'downloads': {...},
  31. 'home_page': 'https://github.com/pypa/sampleproject',
  32. 'keywords': 'sample setuptools development',
  33. 'license': 'MIT',
  34. 'maintainer': None,
  35. 'maintainer_email': None,
  36. 'name': 'sampleproject',
  37. 'package_url': 'https://pypi.org/project/sampleproject/',
  38. 'platform': 'UNKNOWN',
  39. 'project_url': 'https://pypi.org/project/sampleproject/',
  40. 'project_urls': {...},
  41. 'release_url': 'https://pypi.org/project/sampleproject/1.2.0/',
  42. 'requires_dist': None,
  43. 'requires_python': None,
  44. 'summary': 'A sample Python project',
  45. 'version': '1.2.0'}

網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:pprint—-數(shù)據(jù)美化輸出
網(wǎng)頁(yè)路徑:http://m.5511xx.com/article/djhhdcp.html