日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

打印 Python 的一切 —— pprint beeprint

發布時間:2025/3/20 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 打印 Python 的一切 —— pprint beeprint 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

打印,是所有程序員從小白時期就具備的神技,遇事不決打印一下,是 DEBUG 最簡單且不依賴 IDE 的方式,自定義各種日志輸出,也是項目成型后必備功能。

但是為了優雅的打印格式,往往需要對各種對象進行特殊格式化,可遇到字典和大段字符串也不好處理。

這篇文章介紹兩個庫模塊,可以快速有效的解決所有 Python 對象的打印輸出,沒錯,所有。

pprint

這是 Python 標準庫模塊,全稱 pretty printer,可以讓各種數據結構更美觀地輸出。

>>> print(game) {'players': [{'name': 'player_1', 'hp': 3}, {'name': 'player_2', 'hp': 5}]} >>> pprint.pprint(game, width=4) {'players': [{'hp': 3,'name': 'player_1'},{'hp': 5,'name': 'player_2'}]}

pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False)

  • stream:輸出流,默認是 sys.stdout ,也就是屏幕輸出。
  • indent:縮進空格數。
  • width:每行最大寬度,默認為80個字符,超過寬度會換行,但如果單個對象超過不會被換行,比如一段長字符串。
  • depth:限制數據的層級,設置后可以限制過多的嵌套。
  • compact:3.4版本新加入的參數,若設為 True ,則輸出會在接近 width 限制才進行換行。
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' >>> pprint.pprint(stuff, indent=4) [ ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],'spam','eggs','lumberjack','knights','ni'] >>> pprint.pprint(stuff, width=41, compact=True) [['spam', 'eggs', 'lumberjack','knights', 'ni'],'spam', 'eggs', 'lumberjack', 'knights','ni']

pprint.pformat(object, indent=1, width=80, depth=None, *, compact=False)

和 pprint.pprint 的區別是,該函數不會打印到輸出流,而是返回一個格式化字符串。

>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] >>> stuff.insert(0, stuff) >>> pprint.pprint(stuff) [<Recursion on list with id=...>,'spam','eggs','lumberjack','knights','ni']

classpprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False)

上文兩個函數,實際都是構建一個 PrettyPrinter 對象,并調用對應的方法,所以當需要復用格式化的時候,我們可以提前構建該對象。

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' >>> pp = pprint.PrettyPrinter(width=40, compact=True) >>> pp.pprint(game) {'players': [{'hp': 3,'name': 'player_1'},{'hp': 5,'name': 'player_2'}]} >>> pp.pprint(stuff) ['spam', 'eggs', 'lumberjack','knights', 'ni']

pprint 對字典 列表這類數據結構確實很好用,但遇到類、字符串仍然和 print 差別不大,因此這里有一個國人寫的第三方庫介紹給大家。

beeprint

除了和 pprint 一樣支持 dict, list, tuple 等常規變量的格式化輸出,還支持 object 變量打印、長文本自動剪切。

下面是作者 README 中和 pprint 的對比:

pprint:

{'entities': {'hashtags': [],'urls': [{'display_url': 'tumblr.com/xnr37hf0yz','expanded_url': 'http://tumblr.com/xnr37hf0yz','indices': [107, 126],'url': 'http://t.co/cCIWIwg'}],'user_mentions': []}}

beeprint:

{'entities': {'hashtags': [],'urls': [{'display_url': 'tumblr.com/xnr37hf0yz','expanded_url': 'http://tumblr.com/xnr37hf0yz','indices': [107, 126],'url': 'http://t.co/cCIWIwg',},],'user_mentions': [],}, }

pprint:

<class 'definition.NormalClassNewStyle'>

beeprint:

''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:579817333 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' class(NormalClassNewStyle):dicts: {},lists: [],static_props: 1,tuple: (1, 2)

pprint:

<definition.NormalClassNewStyle object at 0x7f338e5a9dd0>

beeprint:

instance(NormalClassNewStyle):dicts: {},lists: [],say_hi: 'hello world',static_props: 1,tuple: (1, 2)

pprint:

[['\nThe sky and the earth were at first one blurred entity like an egg. Pangu was born into it.\n \n\tThe separation of the sky and the earth took eighteen thousand years-the yang which was light and pure rose to become the sky, \tand the yin which was heavy and murky\xef\xbc\x88\xe6\x9c\xa6\xe8\x83\xa7\xe7\x9a\x84\xef\xbc\x89 sank to form the earth. Between them was Pangu, who went through nine \tchanges every day, his wisdom greater than that of the sky and his ability greater than that of the earth. Every day the sky rose ten feet higher, the earth became ten feet thicker, and Pangu grew ten feet taller.\n \nAnother eighteen thousand years passed, and there was an extremely high sky, an extremely thick earth, and an extremely tall Pangu. After Pangu died, his head turned into the Five Sacred Mountains (Mount Tai, Mount Heng, Mount Hua, Mount Heng, Mount Song), his eyes turned into the moon and the sun, his blood changed into water in river and sea, his hair into grass.\n \nIn all, the universe and Pangu combine in one.\n','\n\xe6\x8d\xae\xe6\xb0\x91\xe9\x97\xb4\xe7\xa5\x9e\xe8\xaf\x9d\xe4\xbc\xa0\xe8\xaf\xb4\xe5\x8f\xa4\xe6\x97\xb6\xe7\x9b\x98\xe5\x8f\xa4\xe7\x94\x9f\xe5\x9c\xa8\xe9\xbb\x91\xe6\x9a\x97\xe5\x9b\xa2\xe4\xb8\xad\xef\xbc\x8c\xe4\xbb\x96\xe4\xb8\x8d\xe8\x83\xbd\xe5\xbf\x8d\xe5\x8f\x97\xe9\xbb\x91\xe6\x9a\x97\xef\xbc\x8c\xe7\x94\xa8\xe7\xa5\x9e\xe6\x96\xa7\xe5\x8a\x88\xe5\x90\x91\xe5\x9b\x9b\xe6\x96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a4\xa7\xe5\x9c\xb0\xe4\xb9\x9f\xe5\x8f\x98\xe5\xbe\x97\xe5\x8e\x9a\xe5\xae\x9e\xe6\x97\xa0\xe6\xaf\x94\xe3\x80\x82\xe7\x9b\x98\xe5\x8f\xa4\xe7\x94\x9f\xe5\x89\x8d\xe5\xae\x8c\xe6\x88\x90\xe5\xbc\x80\xe5\xa4\xa9\xe8\xbe\x9f\xe5\x9c\xb0\xe7\x9a\x84\xe4\xbc\x9f\xe5\xa4\xa7\xe4\xb8\x9a\xe7\xbb\xa9\xef\xbc\x8c\xe6\xad\xbb\xe5\x90\x8e\xe6\xb0\xb8\xe8\xbf\x9c\xe7\x95\x99\xe7\xbb\x99\xe5\x90\x8e\xe4\xba\xba\xe6\x97\xa0\xe7\xa9\xb7\xe6\x97\xa0\xe5\xb0\xbd\xe7\x9a\x84\xe5\xae\x9d\xe8\x97\x8f\xef\xbc\x8c\xe6\x88\x90\xe4\xb8\xba\xe4\xb8\xad\xe5\x8d\x8e\xe6\xb0\x91\xe6\x97\x8f\xe5\xb4\x87\xe6\x8b\x9c\xe7\x9a\x84\xe8\x8b\xb1\xe9\x9b\x84\xe3\x80\x82\n']]

beeprint:

[['\nThe sky and the earth were at first one blurred entity like an egg. Panguwas born into it.\n \n\tThe separation of the sky and the earth took...(12 hidden lines)','\n據民間神話傳說古時盤古生在黑暗團中,他不能忍受黑暗,用神斧劈向四方,逐漸使天空高遠,大地遼闊。\n\t他為不使天地會重新合并,繼續施展法術。每當盤古的...(3 hidden lines)',], ]

beeprint.pp(o, output=True, max_depth=5, indent=2, width=80, sort_keys=True, config=None, **kwargs):

參數和 pprint 類似,多出了 sort_keys可以打印字典時是否按 key 排序( pprint 是默認排序,不可選),另外還有控制文本裁剪等行為的參數,可以從 beeprint.Config 里查看。

總結

以上是生活随笔為你收集整理的打印 Python 的一切 —— pprint beeprint的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。