📜  pprint:Python中的数据漂亮打印方法pprint

📅  最后修改于: 2020-04-10 01:04:15             🧑  作者: Mango

本文介绍了Python中一个非常有用的内置模块pprint
pprint模块提供了能力“漂亮打印”任意Python数据结构的格式良好的,更可读的方式!
让我们考虑一个例子:

# 没有pprint的Python代码
import requests
def geocode(address):
    url = "https://maps.googleapis.com/maps/api/geocode/json" # 需要翻墙
    resp = requests.get(url, params = {'address': address})
    return resp.json()
# 调用地址解析功能
data = geocode('India gate')
# 打印json响应
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'}]}

如您所见,此输出未正确缩进,这会影响嵌套数据结构的可读性。
现在,考虑以下代码:

# 带有pprint的Python代码
import requests
from pprint import pprint
def geocode(address):
    url = "https://maps.googleapis.com/maps/api/geocode/json"
    resp = requests.get(url, params = {'address': address})
    return resp.json()
# 调用地址解析功能
data = geocode('India gate')
# pprint漂亮打印json响应
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()函数而不是print函数!