自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

讓我們來談?wù)刾ython中的prettyprint和pprint

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

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

[[344825]]

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

  1. print(“Hello World”) 

但是,在使用print()函數(shù)打印字典、列表或任何其他復(fù)雜數(shù)據(jù)類型時,您是否遇到過這種痛苦呢?由于不適當(dāng)?shù)目s進(jìn)問題,我們經(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. 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)更具可讀性。

讓我們來談?wù)刾ython中的prettyprint

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:

  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=1width=80depth=None
  2.  stream=None, *, compact=Falsesort_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. 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)容。

​英文原文:

https://medium.com/dev-genius/lets-talk-about-prettyprint-or-pprint-in-python-ddda1fa4cf0b​

 

責(zé)任編輯:趙寧寧 來源: Python學(xué)會
相關(guān)推薦

2021-12-16 12:01:21

區(qū)塊鏈Libra貨幣

2018-03-13 15:33:14

虛擬化備份虛擬機(jī)

2013-11-27 09:38:31

云計算虛擬化

2012-04-14 20:47:45

Android

2015-12-30 10:33:12

2012-08-23 09:35:46

2021-08-05 05:02:04

DPU數(shù)據(jù)中心Pensando

2018-08-15 14:02:19

ODCCIT領(lǐng)域液冷

2024-09-29 09:02:17

Go語言類型

2014-09-29 09:00:49

阿里云阿里云開發(fā)者大會

2019-06-13 16:12:35

Android QGoogle智能手機(jī)

2021-11-04 18:15:55

下載上傳瀏覽器

2020-11-27 06:44:22

原子加鎖x86

2010-01-04 14:37:46

Linux Ubunt

2015-08-03 10:10:29

2012-09-13 14:22:16

云計算智慧云城市

2011-07-27 10:30:21

活動目錄

2021-10-20 14:04:10

代碼注釋接口

2021-01-20 15:31:00

區(qū)塊鏈比特幣數(shù)字貨幣
點贊
收藏

51CTO技術(shù)棧公眾號