讓我們來談?wù)刾ython中的prettyprint和pprint
當(dāng)你開始學(xué)習(xí)python編程的時候,你做的第一件事是什么?
相信我們都已經(jīng)通過“Hello World”程序開始了我們的python之旅。在python中,它可以在一行中完成:
- print(“Hello World”)
但是,在使用print()函數(shù)打印字典、列表或任何其他復(fù)雜數(shù)據(jù)類型時,您是否遇到過這種痛苦呢?由于不適當(dāng)?shù)目s進(jìn)問題,我們經(jīng)常在python嵌套數(shù)據(jù)結(jié)構(gòu)的輸出中遇到可讀性方面的困難。
讓我們在這里試試代碼:
- coordinates = [
- {
- “name”: “Location 1”,
- “gps”: (29.008966, 111.573724)
- },
- {
- “name”: “Location 2”,
- “gps”: (40.1632626, 44.2935926)
- },
- {
- “name”: “Location 3”,
- “gps”: (29.476705, 121.869339)
- }
- ]
- print(coordinates)
以上代碼的輸出:
- [{‘gps’: (29.008966, 111.573724), ‘name’:
- ‘Location 1’}, {‘gps’: (40.1632626, 44.2935926),
- ‘name’: ‘Location 2’}, {‘gps’: (29.476705, 121.869339),
- ‘name’: ‘Location 3’}]
我們可以看到,上面的代碼不容易讀懂,也不美觀。
如果解決這個問題呢?
Python附帶pprint模塊,可以讓打印任意數(shù)據(jù)結(jié)構(gòu)更具可讀性。

pprint是什么?
python中的pprint模塊負(fù)責(zé)以合適的格式打印便于閱讀的行塊。它使用換行和縮進(jìn)以明確的方式打印數(shù)據(jù)。
pprint與print有何不同?
print()是python中的一個簡單函數(shù),用于在屏幕上向用戶顯示指定的消息。但通常,如果我們使用python打印一個字典、列表或任何其他復(fù)雜的函數(shù),我們會發(fā)現(xiàn)讀取打印出來的語句是模棱兩可的。它包括內(nèi)置的對象、文件、套接字、類或?qū)嵗?,這些不能用Python常量表示。
然后,“pprint”模塊可以幫助您。
它將對象格式化為可讀的格式,每行都有適當(dāng)?shù)膶挾取K鼛в锌烧{(diào)節(jié)的寬度限制,以使它更容易為用戶。它將所有元素轉(zhuǎn)換為可以用Python常量表示的字符串,并以美觀的格式打印它們。pprint函數(shù)返回可以在解釋器中作為輸入運行的輸出。而且,通過解析字符串更容易將其轉(zhuǎn)換回該類型。
那么,讓我們深入pprint…
在python文件的頂部導(dǎo)入庫pprint:
- import pprint
現(xiàn)在,我們可以使用.pprint()對象或?qū)嵗覀冏约旱膒print對象PrettyPrinter()。
- pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])
- # Instantiating pprint object
- my_pprint = pprint.PrettyPrinter()
- my_pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])
兩個打印函數(shù)給出的結(jié)果是一致的
- ['Radha', 1, 'Hari', 'Simesh', 25, 847]
- ['Radha', 1, 'Hari', 'Simesh', 25, 847]
那么,pprint()和PrettyPrinter()之間的區(qū)別是什么?
如果需要調(diào)整寬度約束或其他參數(shù),則顯式地構(gòu)造PrettyPrinter對象。
語法:
- class pprint.PrettyPrinter(indent=1, width=80, depth=None,
- stream=None, *, compact=False, sort_dicts=True)
pprint()方法使用庫的默認(rèn)設(shè)置,而在創(chuàng)建PrettyPrinter()對象時,我們可以更改庫的默認(rèn)配置。這就是二者之間的區(qū)別。
讓我們通過幾個例子來理解:
深度參數(shù)決定多遠(yuǎn)一個Python PrettyPrinter遞歸嵌套結(jié)構(gòu):
- tuple1 = ('spam', ('eggs', ('lumberjack', ('knights',
- ('ni', ('dead',('parrot', ('fresh fruit',))))))))
- # Using PrettyPrinter
- pp = pprint.PrettyPrinter(depth=6) # default configuration
- # of depthbeing none is changed to depth = 6
- # Now it will print till depth of six brackets
- pp.pprint(tuple1)
- #Using only pprint() object
- pprint.pprint(pprint.pprint(tuple1, depth=6))
- pprint.pprint(tuple1)
以上代碼的輸出:
- ('spam', ('eggs', ('lumberjack', ('knights', ('ni',
- ('dead', (...)))))))
- ('spam', ('eggs', ('lumberjack', ('knights', ('ni',
- ('dead', (...)))))))
- ('spam',
- ('eggs',
- ('lumberjack', ('knights', ('ni', ('dead',
- ('parrot', ('fresh fruit',))))))))
你能注意到其中的區(qū)別嗎?
我們看到,當(dāng)tuple1打印使用深度= 6,之后六個橢圓打印的時候是沒有更多的數(shù)據(jù)顯示而只使用pprint.pprint(),那么所有的數(shù)據(jù)顯示。
設(shè)置不存儲在.pprint()中,即默認(rèn)設(shè)置保持不變,而在PrettyPrinter()中,設(shè)置或更改是存儲的。這里存儲的是depth = 6。
使用寬度參數(shù),我們可以選擇輸出將打印多少列。默認(rèn)寬度是80,也就是80個字符,但是你可以改變它。
現(xiàn)在,讓我們看看在幾個內(nèi)置函數(shù)中使用pprint()比print()函數(shù)的主要區(qū)別和好處。
- from pprint import pprint # We can directly call the method
- # pprint() using it
- coordinates = [
- {
- “name”: “Location 1”,
- “gps”: (29.008966, 111.573724)
- },
- {
- “name”: “Location 2”,
- “gps”: (40.1632626, 44.2935926)
- },
- {
- “name”: “Location 3”,
- “gps”: (29.476705, 121.869339)
- }
- ]
- pprint(coordinates)
輸出:
- [{'gps': (29.008966, 111.573724), 'name': 'Location 1'},
- {'gps': (40.1632626, 44.2935926), 'name': 'Location 2'},
- {'gps': (29.476705, 121.869339), 'name': 'Location 3'}]
這就是本文中關(guān)于python中的pprint的全部內(nèi)容。
英文原文:
https://medium.com/dev-genius/lets-talk-about-prettyprint-or-pprint-in-python-ddda1fa4cf0b