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