pprint:Python中的数据打印机

这篇文章是关于Python中一个非常有用的内置模块, pprint .

null

这个 pprint 模块提供了以格式良好且可读性更强的方式“漂亮地打印”任意Python数据结构的功能!

让我们考虑一个例子:

# A python code without pprint
import requests
def geocode(address):
resp = requests.get(url, params = { 'address' : address})
return resp.json()
# calling geocode function
data = geocode( 'India gate' )
# printing json response
print (data)


以上代码用于使用JSON格式的Google Maps API获取某个地方的地理代码信息。

上述程序的输出如下所示:

{'status': 'OK', 'results': [{'address_components': [{'long_name': 'Rajpath', 'types': ['route'], 'short_name': 'Rajpath'}, {'long_name': 'India Gate', 'types': ['political', 'sublocality', 'sublocality_level_1'], 'short_name': 'India Gate'}, {'long_name': 'New Delhi', 'types': ['locality', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'New Delhi', 'types': ['administrative_area_level_2', 'political'], 'short_name': 'New Delhi'}, {'long_name': 'Delhi', 'types': ['administrative_area_level_1', 'political'], 'short_name': 'DL'}, {'long_name': 'India', 'types': ['country', 'political'], 'short_name': 'IN'}, {'long_name': '110001', 'types': ['postal_code'], 'short_name': '110001'}], 'geometry': {'location': {'lng': 77.2295097, 'lat': 28.612912}, 'viewport': {'northeast': {'lng': 77.2308586802915, 'lat': 28.6142609802915}, 'southwest': {'lng': 77.22816071970848, 'lat': 28.6115630197085}}, 'location_type': 'APPROXIMATE'}, 'types': ['establishment', 'point_of_interest'], 'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi 110001, India', 'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc'}]}

如您所见,此输出没有正确缩进,这会影响嵌套数据结构的可读性。

现在,考虑下面的代码:

# A python code with pprint
import requests
from pprint import pprint
def geocode(address):
resp = requests.get(url, params = { 'address' : address})
return resp.json()
# calling geocode function
data = geocode( 'India gate' )
# pretty-printing json response
pprint(data)


上述代码的输出如下所示:

{'results': [{'address_components': [{'long_name': 'Rajpath',                                      'short_name': 'Rajpath',                                      'types': ['route']},                                     {'long_name': 'India Gate',                                      'short_name': 'India Gate',                                      'types': ['political',                                                'sublocality',                                                'sublocality_level_1']},                                     {'long_name': 'New Delhi',                                      'short_name': 'New Delhi',                                      'types': ['locality', 'political']},                                     {'long_name': 'New Delhi',                                      'short_name': 'New Delhi',                                      'types': ['administrative_area_level_2',                                                'political']},                                     {'long_name': 'Delhi',                                      'short_name': 'DL',                                      'types': ['administrative_area_level_1',                                                'political']},                                     {'long_name': 'India',                                      'short_name': 'IN',                                      'types': ['country', 'political']},                                     {'long_name': '110001',                                      'short_name': '110001',                                      'types': ['postal_code']}],              'formatted_address': 'Rajpath, India Gate, New Delhi, Delhi '                                   '110001, India',              'geometry': {'location': {'lat': 28.612912, 'lng': 77.2295097},                           'location_type': 'APPROXIMATE',                           'viewport': {'northeast': {'lat': 28.6142609802915,                                                      'lng': 77.2308586802915},                                        'southwest': {'lat': 28.6115630197085,                                                      'lng': 77.22816071970848}}},              'place_id': 'ChIJC03rqdriDDkRXT6SJRGXFwc',              'types': ['establishment', 'point_of_interest']}], 'status': 'OK'}

如您所见,输出现在格式良好,可读性更强。

我们所做的只是进口 pprint 功能 pprint 单元使用 pprint() 功能而不是 打印 作用

本博客由 尼希尔·库马尔 .如果你喜欢GeekSforgeks并想贡献自己的力量,你也可以用write写一篇文章。极客。组织或邮寄你的文章进行评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享