模块:import pprint
作用:"pretty-print"的缩写,涉及到字典或列表的输出,使用print输出看起来并不美观,用pprint输出方便查看输出
import pprint
list1=[{'name':'abc','height':180,'weight':'65kg'},{'name':'cde','height':170,'weight':'52.5kg'},{'name':'def','height':169,'weight':'50.3kg'},{'name':'hik','height':173.6,'weight':'60kg'}]
print(list1)
对于列表和字典的输出,用print打印如下:
[{'name': 'abc', 'height': 180, 'weight': '65kg'}, {'name': 'cde', 'height': 170, 'weight': '52.5kg'}, {'name': 'def', 'height': 169, 'weight': '50.3kg'}, {'name': 'hik', 'height': 173.6, 'weight': '60kg'}]
而利用pprint函数
import pprint
list1=[{'name':'abc','height':180,'weight':'65kg'},{'name':'cde','height':170,'weight':'52.5kg'},{'name':'def','height':169,'weight':'50.3kg'},{'name':'hik','height':173.6,'weight':'60kg'}]
pprint.pprint(list1)
输出结果如下:
[{'height': 180, 'name': 'abc', 'weight': '65kg'},
{'height': 170, 'name': 'cde', 'weight': '52.5kg'},
{'height': 169, 'name': 'def', 'weight': '50.3kg'},
{'height': 173.6, 'name': 'hik', 'weight': '60kg'}]
美观,易于查找想要的数据