python中pprint_Python中的pprint(漂亮打印机)

python中pprint

pprint (pprint)

pprint is a python module that helps us to make the readability of the complex data structures easy. The pprint is also called as "pretty print".

pprint是一个python模块,可帮助我们简化 复杂数据结构的可读性pprint也称为“漂亮打印”

Let us consider an example,

让我们考虑一个例子,

dictionary={'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 
'haze', 'icon': '50d'}], 'base': 'stations', 'main': 
{'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 
'visibility': 6000, 'wind': {'speed': 4.1, 'deg': 290, 
'gust': 9.3}, 'clouds': {'all': 30}, 'dt': 1590398990, 
'sys': {'type': 1, 'id': 9165, 'country': 'IN', 
'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 
'cod': 200}

# This is the dictionary that we wanted to print
print(dictionary)

Now request module is of the article its just an example to create a nested data structure.

现在,本文的请求模块只是创建嵌套数据结构的一个示例。

Output:

输出:

{'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 'haze', 'icon': '50d'}], 
'base': 'stations', 'main': {'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 'visibility': 6000, 
'wind': {'speed': 4.1, 'deg': 290, 'gust': 9.3}, 'clouds': {'all': 30}, 
'dt': 1590398990, 'sys': {'type': 1, 'id': 9165, 'country': 'IN', 'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 'cod': 200}

As you can see, the output is not in a proper and readable way, we cannot read this complex nested dictionary structure.

如您所见, 输出的格式不正确且可读 ,我们无法读取此复杂的嵌套字典结构。

To solve this issue of readability we will use the inbuilt module pprint.

为了解决此可读性问题,我们将使用内置模块pprint

下载pprint模块 (Downloading pprint Module)

General way: In your terminal or command prompt, type the following command,

常规方法:在终端或命令提示符下,键入以下命令,

pip install pprint

Using pycharm: Go to the project interpreter and install the module.

使用pycharm:转到项目解释器并安装模块。

Now after installing, import the module and there is a function in this module named pprint so import that as

现在,安装后,导入模块, 此模块中有一个名为pprint函数,因此将其导入为

from pprint import pprint

To make the structure look good just pprint() instead of print().

为了使结构看起来不错,只需使用pprint()而不是print()即可

# import pprint from the module pprint
from pprint import pprint


dictionary={'coord': {'lon': 77.22, 'lat': 28.67}, 
'weather': [{'id': 721, 'main': 'Haze', 'description': 
'haze', 'icon': '50d'}], 'base': 'stations', 'main': 
{'temp': 44, 'feels_like': 40.42, 'temp_min': 44, 
'temp_max': 44, 'pressure': 1002, 'humidity': 11}, 
'visibility': 6000, 'wind': {'speed': 4.1, 'deg': 290, 
'gust': 9.3}, 'clouds': {'all': 30}, 'dt': 1590398990, 
'sys': {'type': 1, 'id': 9165, 'country': 'IN', 
'sunrise': 1590364538, 'sunset': 1590414050}, 
'timezone': 19800, 'id': 1273294, 'name': 'Delhi', 
'cod': 200}

# This is the dictionary that we wanted to print
pprint(dictionary)

Output:

输出:

{'base': 'stations',
 'clouds': {'all': 30},
 'cod': 200,
 'coord': {'lat': 28.67, 'lon': 77.22},
 'dt': 1590398990,
 'id': 1273294,
 'main': {'feels_like': 40.42,
          'humidity': 11,
          'pressure': 1002,
          'temp': 44,
          'temp_max': 44,
          'temp_min': 44},
 'name': 'Delhi',
 'sys': {'country': 'IN',
         'id': 9165,
         'sunrise': 1590364538,
         'sunset': 1590414050,
         'type': 1},
 'timezone': 19800,
 'visibility': 6000,
 'weather': [{'description': 'haze', 'icon': '50d', 'id': 721, 'main': 'Haze'}],
 'wind': {'deg': 290, 'gust': 9.3, 'speed': 4.1}}
 

The above output is clear and it is easily readable.

上面的输出清晰易读。

翻译自: https://www.includehelp.com/python/pprint-pretty-printer.aspx

python中pprint

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值