日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
讓我們來談?wù)刾ython中的prettyprint和pprint

當(dāng)你開始學(xué)習(xí)python編程的時候,你做的第一件事是什么?

相信我們都已經(jīng)通過“Hello World”程序開始了我們的python之旅。在python中,它可以在一行中完成:

 
 
 
  1. print(“Hello World”)

但是,在使用print()函數(shù)打印字典、列表或任何其他復(fù)雜數(shù)據(jù)類型時,您是否遇到過這種痛苦呢?由于不適當(dāng)?shù)目s進問題,我們經(jīng)常在python嵌套數(shù)據(jù)結(jié)構(gòu)的輸出中遇到可讀性方面的困難。

讓我們在這里試試代碼:

 
 
 
  1. coordinates = [
  2.  {
  3.  “name”: “Location 1”,
  4.  “gps”: (29.008966, 111.573724)
  5.  },
  6.  {
  7.  “name”: “Location 2”,
  8.  “gps”: (40.1632626, 44.2935926)
  9.  },
  10.  {
  11.  “name”: “Location 3”,
  12.  “gps”: (29.476705, 121.869339)
  13.  }
  14. ]
  15. print(coordinates)

以上代碼的輸出:

 
 
 
  1. [{‘gps’: (29.008966, 111.573724), ‘name’: 
  2. ‘Location 1’}, {‘gps’: (40.1632626, 44.2935926), 
  3. ‘name’: ‘Location 2’}, {‘gps’: (29.476705, 121.869339), 
  4. ‘name’: ‘Location 3’}]

我們可以看到,上面的代碼不容易讀懂,也不美觀。

如果解決這個問題呢?

Python附帶pprint模塊,可以讓打印任意數(shù)據(jù)結(jié)構(gòu)更具可讀性。

pprint是什么?

python中的pprint模塊負(fù)責(zé)以合適的格式打印便于閱讀的行塊。它使用換行和縮進以明確的方式打印數(shù)據(jù)。

pprint與print有何不同?

print()是python中的一個簡單函數(shù),用于在屏幕上向用戶顯示指定的消息。但通常,如果我們使用python打印一個字典、列表或任何其他復(fù)雜的函數(shù),我們會發(fā)現(xiàn)讀取打印出來的語句是模棱兩可的。它包括內(nèi)置的對象、文件、套接字、類或?qū)嵗?,這些不能用Python常量表示。

然后,“pprint”模塊可以幫助您。

它將對象格式化為可讀的格式,每行都有適當(dāng)?shù)膶挾?。它帶有可調(diào)節(jié)的寬度限制,以使它更容易為用戶。它將所有元素轉(zhuǎn)換為可以用Python常量表示的字符串,并以美觀的格式打印它們。pprint函數(shù)返回可以在解釋器中作為輸入運行的輸出。而且,通過解析字符串更容易將其轉(zhuǎn)換回該類型。

那么,讓我們深入pprint…

在python文件的頂部導(dǎo)入庫pprint:

 
 
 
  1. import pprint

現(xiàn)在,我們可以使用.pprint()對象或?qū)嵗覀冏约旱膒print對象PrettyPrinter()。

 
 
 
  1. pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])
  2. # Instantiating pprint object
  3. my_pprint = pprint.PrettyPrinter()
  4. my_pprint.pprint(['Radha', 1, 'Hari', 'Simesh', 25, 847])

兩個打印函數(shù)給出的結(jié)果是一致的

 
 
 
  1. ['Radha', 1, 'Hari', 'Simesh', 25, 847] 
  2. ['Radha', 1, 'Hari', 'Simesh', 25, 847]

那么,pprint()和PrettyPrinter()之間的區(qū)別是什么?

如果需要調(diào)整寬度約束或其他參數(shù),則顯式地構(gòu)造PrettyPrinter對象。

語法:

 
 
 
  1. class pprint.PrettyPrinter(indent=1, width=80, depth=None,
  2.  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):

 
 
 
  1. tuple1 = ('spam', ('eggs', ('lumberjack', ('knights', 
  2. ('ni', ('dead',('parrot', ('fresh fruit',))))))))
  3. # Using PrettyPrinter
  4. pp = pprint.PrettyPrinter(depth=6) # default configuration 
  5. # of depthbeing none is changed to depth = 6
  6. # Now it will print till depth of six brackets
  7. pp.pprint(tuple1)
  8. #Using only pprint() object
  9. pprint.pprint(pprint.pprint(tuple1, depth=6))
  10. pprint.pprint(tuple1)

以上代碼的輸出:

 
 
 
  1. ('spam', ('eggs', ('lumberjack', ('knights', ('ni', 
  2. ('dead', (...))))))) 
  3. ('spam', ('eggs', ('lumberjack', ('knights', ('ni', 
  4. ('dead', (...)))))))
  5. ('spam',
  6.   ('eggs',
  7.    ('lumberjack', ('knights', ('ni', ('dead', 
  8.    ('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ū)別和好處。

 
 
 
  1. from pprint import pprint # We can directly call the method 
  2. # pprint() using it
  3. coordinates = [
  4.  {
  5.  “name”: “Location 1”,
  6.  “gps”: (29.008966, 111.573724)
  7.  },
  8.  {
  9.  “name”: “Location 2”,
  10.  “gps”: (40.1632626, 44.2935926)
  11.  },
  12.  {
  13.  “name”: “Location 3”,
  14.  “gps”: (29.476705, 121.869339)
  15.  }
  16. ]
  17. pprint(coordinates)

輸出:

 
 
 
  1. [{'gps': (29.008966, 111.573724), 'name': 'Location 1'},
  2.  {'gps': (40.1632626, 44.2935926), 'name': 'Location 2'},
  3.  {'gps': (29.476705, 121.869339), 'name': 'Location 3'}]

這就是本文中關(guān)于python中的pprint的全部內(nèi)容。


文章標(biāo)題:讓我們來談?wù)刾ython中的prettyprint和pprint
文章出自:http://www.dlmjj.cn/article/djchjjc.html