python库——pprint

python库——pprint

pprint库简介:

python库查询
pprint库很好使用,它提供pprint()和pformat()两种费中好用的函数。

pprint库安装:

在这里插入图片描述
直接上手pip isntall pprint会出现错误,所以要换一个源。

pip install pprint -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

pprint()

pprint()将输入的对象直接打印到控制台。

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

解释其中几个参数:

  1. object:数据对象,List、dict这两类数据结构,当然其他的数据结构能不能接受我还没有探索过,要是有的话可以跟我说。
  2. width:每次打印出的一行的字母宽度
  3. sort_dicts:pprint()函数会对object对象的字典序进行排列。
实例:

在这里插入图片描述

>>> import pprint
>>> company = {
...     "Apple":    {"founder": "Steven Jobs", "prodctions":["IPhone", "Mac"]},
...     "MicroSoft":{"founder": "Bill Gates", "prodctions": ["Windows", "Office","Xbox"]},
... }
>>> pprint.pprint(company)
{'Apple': {'founder': 'Steven Jobs', 'prodctions': ['IPhone', 'Mac']},
 'MicroSoft': {'founder': 'Bill Gates',
               'prodctions': ['Windows', 'Office', 'Xbox']}}
>>> print(company)
{'Apple': {'founder': 'Steven Jobs', 'prodctions': ['IPhone', 'Mac']}, 'MicroSoft': {'founder': 'Bill Gates', 'prodctions': ['Windows', 'Office', 'Xbox']}}
>>>

未完待续


  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值