python:pprint实例

为了演示 pprint() 函数及其形参的几种用法,让我们从 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']

pprint() 以其基本形式显示了整个对象:

>>>
pprint.pprint(project_info)
{'author': 'The Python Packaging Authority','author_email': 'pypa-dev@googlegroups.com','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'}

结果可以被限制到特定的 depth (更深层的内容将使用省略号):

>>>
pprint.pprint(project_info, depth=1)
{'author': 'The Python Packaging Authority','author_email': 'pypa-dev@googlegroups.com','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'}

此外,还可以设置建议的最大字符 width。 如果一个对象无法被拆分,则将超出指定宽度:

>>>
pprint.pprint(project_info, depth=1, width=60)
{'author': 'The Python Packaging Authority','author_email': 'pypa-dev@googlegroups.com','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'}

python:pprint实例相关推荐

  1. 用python实现视频换脸_超简单使用Python换脸实例

    换脸! 这段时间,deepfakes搞得火热,比方说把<射雕英雄传>里的朱茵换成了杨幂,看下面的图!毫无违和感! 其实早在之前,基于AI换脸的技术就得到了应用,比方说<速度与激情7& ...

  2. python简单编程例子-python简单实例训练(21~30)

    注意:我用的python2.7,大家如果用Python3.0以上的版本,请记得在print()函数哦!如果因为版本问题评论的,不做回复哦!! 21.题目:将一个正整数分解质因数.例如:输入90,打印出 ...

  3. python脚本实例手机端-python链接手机用Python实现命令行闹钟脚本实例

    前言: 这篇文章给大家介绍了怎样用python创建一个简单的报警,它可以运行在命令行终端,它需要分钟做为命令行参数,在这个分钟后会打印"wake-up"消息,并响铃报警,你可以用0 ...

  4. python爬虫实例-记录一次简单的Python爬虫实例

    本次的这篇文章主要是和大家分享了一篇关于记录一次简单的Python爬虫实例 ,有需要的小伙伴可以看一下. 主要流程分为: 爬取.整理.存储 1.其中用到几个包,包括 requests 用于向网站发送请 ...

  5. python经典案例-Python经典实例

    本书是Python经典实例解析,采用基于实例的方法编写,每个实例都会解决具体的问题和难题.主要内容有:数字.字符串和元组,语句与语法,函数定义,列表.集.字典,用户输入和输出等内置数据结构,类和对象, ...

  6. python程序格式框架的描述_python 程序语言设计(嵩天)-学习笔记(第二章python 程序实例解析)...

    第 2 章 python 程序实例解析 学习目标: 掌握解决计算问题的一般方法. 掌握python语言的基本语法,包括缩进.变量.命名等. 掌握python语言绘制图形的一般方法. 了解python标 ...

  7. 涵盖 14 大主题!最完整的 Python 学习实例集来了!

    机器学习.深度学习最简单的入门方式就是基于 Python 开始编程实战.最近闲逛 GitHub,发现了一个非常不错的 Python 学习实例集,完全是基于 Python 来实现包括 ML.DL 等领域 ...

  8. python组成不重复的三位数是多少_超星Python 练习实例1-组成多少个互不相同且无重复的三位数字...

    数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. 程序源代码: #!/u ...

  9. redis实例python_生产消费者模式与python+redis实例运用(基础篇)

    根据这个图,我们举个简单的例子:假如你去某个餐厅吃饭,点了很多菜,厨师要一个一个菜的做,一个厨师不可能同时做出所有你点的菜,于是你有两个选择:第一个,厨师把所有菜都上齐了,你才开始吃:还有一个选择,做 ...

最新文章

  1. c语言程序 实现简单计算器功能,C语言实现简单计算器小项目
  2. 定制AjaxControlToolkit(1):使一个CalendarExtender对应N个TextBox
  3. 【转载】安卓APP架构
  4. 前端用Sass实现星级评定效果,简单快捷实现星级切换。
  5. DP:***24种设计模式--转自刘伟
  6. 面向对象(方法的形式参数)
  7. 论文浅尝 | 基于神经网络的实体识别和关系抽取联合学习
  8. ABP理论学习之日志记录
  9. (03)Verilog HDL模块例化
  10. .net mvc ajax重定向,如何在MVC3 JQuery的Ajax调用重定向到新的页面(连同模型)(How to redirec...
  11. Python黑帽子 黑客与渗透测试编程之道(三)取代netcat
  12. 前言:电商产品经理必修课-如何打造实战型商品系统
  13. java初学者学习路线
  14. python面向对象学习
  15. wifidog 配置中文说明
  16. div实现拖动时候出现禁止图标解决——e.preventDefault e.preventDefault();
  17. raft协议对网络分区的处理
  18. c罗说什么语言,那些年属于C罗的经典语录,那一句话最感动你?
  19. iceberg-flink 十:flink 窗口,事件时间,处理时间。
  20. aws没有免费套餐服务数据可用

热门文章

  1. 基于易语言,PC端微信多开
  2. json php 传输,使用JSON实现数据的跨域传输的php代码
  3. HTML+CSS绘制太阳系九大行星运行轨迹
  4. MATLAB学习笔记-IEEE802.16d模型
  5. 后台执行linux命令
  6. JavaWEB-04 项目案例(1)
  7. ehvierwer登录与不登录_微信号提示“系统检测有被盗风险”登录不上去该如何解决?...
  8. 降维方法(LDA、PCA、KLDA、MDS和ISOMAP)总结
  9. 【HTML501】HTML基础01_简介_基础_元素_属性
  10. Jenkins Git Changelog Plugin