本身的参数的情况下,可以在引入 pprint 模块后,写上 “print = pprint.pprint”,令 print() 起到改头换面的效果:
import pprint
print = pprint.pprint
mylist = ["Beautiful is better than ugly.", "Explicit is better than implicit.", "Simple is better than complex.", "Complex is better than complicated."]
print(mylist)
# 可对比本文开头的例子
['Beautiful is better than ugly.',
'Explicit is better than implicit.',
'Simple is better than complex.',
'Complex is better than complicated.']
2、国人开发的 beeprint
国内某位 pan 同学在 Github 开源了个beeprint
,明显是对标 pprint
的。
项目地址:https://github.com/panyanyany/beeprint
它优化了字典对象的打印,对于从其它语言转过来的同学而言(例如 Java),这是个福音:
它还优化了长文本的打印,支持自定义对象的打印,看起来不错。
但是,其它功能不够齐全,而且作者停止维护两年了,荒废已久……
总体而言,pprint 算是 print() 的轻量级替代,简单实用,极其方便(毕竟是标准库),文档丰富而有保障。
所以,若想要打印美观易读的数据,这个 pprint 标准库,不妨一试哦。
作者简介:豌豆花下猫,生于广东毕业于武大,现为苏漂程序员,有一些极客思维,也有一些人文情怀,有一些温度,还有一些态度。公众号:「Python猫」(python_cat)
公众号【Python猫】, 本号连载优质的系列文章,有喵星哲学猫系列、Python进阶系列、好书推荐系列、技术写作、优质英文推荐与翻译等等,欢迎关注哦。