Python pprint模塊

在打印程序中的數據時,我們遇到了以標準格式打印數據集的問題。輸出中打印的數據打印時沒有定義格式,這使得數據看起來沒有組織。此外,我們很難從打印的數據集中尋找特定的信息。如果我們必須以某種設定的格式打印這些數據,我們必須對數據集的每一行和每一列使用多個格式函數。當給定的數據集有多個不同大小的行和列條目時,這將變得耗時且繁忙。因此,如果我們必須在程序的輸出中以某種排序的方式打印數據集,我們必須尋找一些以集合格式打印數據集的替代選擇。當我們從數據集中尋找某些特定信息時,這種設置格式的打印數據集也會有所幫助。這就是為什麼在開發和編程任務中,如此漂亮的打印機軟件包的重要性會增加。許多編程語言為我們提供了包,我們可以使用這些包在輸出中以設定的格式打印數據集。具體來說 Python,它有一個 pprint模塊,該模塊帶有多種功能,用於以某種集合或定義的格式打印數據集。因此,我們將在本教程中研究 Python 的 pprint模塊,並學習如何使用該模塊的功能在輸出中以某種特定格式打印數據集。

pprint模塊是 Python 標準庫的一部分,它附帶了多個函數,用於在輸出中以特定格式打印給定數據集。pprint模塊本身的名稱代表漂亮的打印機模塊,它也被稱為數據漂亮的打印機模塊。由於 pprint模塊的功能,數據結構和數據集的美觀外觀也是可能的。pprint模塊使我們能夠以定義的格式打印任何給定的數據結構,從而在輸出中給它一個美觀的外觀。在 Python 程序中被正確解析和執行的任何數據結構或數據集,在輸出中都被 pprint模塊的功能優雅地格式化。

輸出中數據結構的格式化表達式儘可能由 pprint模塊保持在一行中,但有時當格式化表達式的長度超過定義的長度時,它會被分成多行。有時,當格式化表達式的大小超過 pprint模塊格式化函數的默認寬度參數時,它會分成多行。程序 pprint 輸出中的字典數據在輸出中表示或顯示結果之前會自動格式化,這是 pprint模塊的一個獨特功能。因此,我們可以說使用 pprint模塊打印的數據結構良好,可讀性更強。

pprint模塊自帶 Python 的標準庫,即 pprint 是隨標準 Python 發行版的安裝一起安裝的 Python 預裝模塊。因此,在 Python 程序中使用 pprint模塊之前,我們不必執行任何安裝過程。我們可以直接開始將這個模塊導入 Python 程序來使用它,並了解它的實現和功能。我們可以用下面一行代碼在程序中導入 Python 的 pprint模塊:


# Importing the pprint module
import pprint

因此,我們可以在本教程的示例程序中使用這個模塊來使用它並理解它的實現。

Python 的 pprint模塊在其標準庫中包含了 PrettyPrinter 類的定義,用於以排序格式設置給定的數據結構。要使用這個模塊的 PrettyPrinter 類,我們必須在程序中為這個類定義一個構造器,只有在這之後,我們才能使用這個 PrettyPrinter 類的 pprint模塊的函數。

如果我們必須在程序中使用 pprint 類,我們可以通過在程序中定義該類的構造器來使用它,然後在以某種特定格式打印數據結構或數據集時,將該構造器與 pprint模塊的函數一起使用。

我們必須使用以下語法或代碼行來定義 pprint 類的構造器:


# Constructor for pprint class
pcr = ppr.PrettyPrinter(indentGiven, widthOfLine, depthInData, stream, compact)

參數:從上面寫的語法中我們可以看到,可以使用 pprint模塊的 PrettyPrinter()函數定義 pprint 類的構造器。函數的作用是:當我們使用它來定義 printyprinter 類的構造器時,它接受以下五個參數:

(i)縮進給定:函數的縮進參數用於定義在每個遞歸級別(每行和每列中的數據值)之後應該添加的縮進。如果我們不通過在函數中傳遞一個參數來定義這個參數,函數將採用默認參數,縮進參數為 1。

(ii)行寬:函數的這個參數定義了數據結構中每一行的寬度,數據結構的期望輸出格式被限制在這個格式。width 參數的默認值是 80,當我們沒有在函數中提供任何 width 參數時,就會用到它。如果數據結構行的長度大於函數中定義的寬度參數,它將在輸出中分解成多個行結構。

(iii)深度數據:函數的深度參數用於控制輸出中打印的級別數。它是函數的可選參數,此參數沒有默認值。

(iv)流:流參數的默認值為 std.out-默認輸出機器。此參數在函數中用於在輸出中打印數據結構時獲取任何流對象,如文件。

(v) compact :是 PrettyPrinter()函數中使用的最後一個參數,默認情況下 compact 參數 id 設置為 False。只有當我們顯示的數據在寬度範圍內可調時,我們才需要將該參數設置為 True。

這是在定義 pretyprinter 類的構造器時,在 pretyprinter()函數中應該給出的五個參數。我們可以使用這些參數來指定我們想要在輸出中打印給定數據結構的格式。

pprint模塊有多個方法,其中一些方法是使用 PrettyPrinter 類的構造器定義的。這些由 PrettyPrinter 類的構造器定義的方法或函數在程序中用來以指定的格式打印數據結構。下面是在 PrettyPrinter 類的構造器下定義的兩個方法:

a) pprint()函數:PrettyPrinter 類中的第一個方法是 pprint()函數,它在輸出中打印 PrettyPrinter 對象的格式化表示(其中來自數據結構的數據以特定格式存儲)。我們可以使用這個函數作為 print 語句來打印程序中的任何數據。

b) pformat()函數:基於我們在 pretyprinter 類的構造器中提供的參數,這個函數返回 pretyprinter 對象的格式化表示。

這兩個函數是在 PrettyPrinter 類的構造器下定義的,我們可以將這些方法與構造器一起使用,並在輸出中打印數據結構。

在本教程中,我們已經學習了 pprint模塊的函數、類和方法。現在,我們將在教程的這一部分了解 pprint模塊及其類和函數的工作。我們將在示例程序中使用 pprint模塊的函數和類構造器來理解它們在程序中的功能和工作。我們將使用示例程序中的 pprint()和 pformat()函數來理解它們的實現,以及我們如何使用 pformat()函數根據輸出中指定的格式打印數據結構。

看下面的示例程序,了解打印數據結構的 pprint模塊的功能實現:

在這個實現部分,我們將使用 pprint模塊的 pprint()函數以指定的格式打印程序中給定的數據結構,這樣它看起來會更吸引人和格式化。我們將使用一些示例程序來理解這個 pprint()函數的工作原理,函數中給出了不同的參數。首先,查看下面的示例程序,了解使用 pprint()函數打印的輸出:

示例 1: 看看下面的 Python 程序,我們在其中比較了簡單的 print 語句輸出和 pprint()函數輸出:


# Importing the pprint module as ppr
import pprint as ppr
# Defining a data structure that has students data
studentDS = {"Dwayne Johnson": ["English", "Chemistry", "Physics", "Mathematics", "Biology"],
     "Randy Orton" :{"English": 50,"Physics": 60,
     "Chemistry": 70, "Mathematics": 80, "Biology": 90},
     "Karl Marx" :(50, 60, 70, 80, 90)}
# Defining Constructor for the PrettyPrinter class
ppConst = ppr.PrettyPrinter()
# Printing normal output of the student data structure
print ("Normal output of student data structure with print statement:")
print (studentDS)
print ("----")
# Printing formatted output of the pprint() function
print ("The pprint function's formatted output of the student data structure:")
# Using pprint() function
ppConst.pprint(studentDS)

輸出:

Normal output of student data structure with print statement:
{'Dwayne Johnson': ['English', 'Chemistry', 'Physics', 'Mathematics', 'Biology'], 'Randy Orton': {'English': 50, 'Physics': 60, 'Chemistry': 70, 'Mathematics': 80, 'Biology': 90}, 'Karl Marx': (50, 60, 70, 80, 90)}
----
The pprint function's formatted output of the student data structure:
{'Dwayne Johnson': ['English',
                    'Chemistry',
                    'Physics',
                    'Mathematics',
                    'Biology'],
 'Karl Marx': (50, 60, 70, 80, 90),
 'Randy Orton': {'Biology': 90,
                 'Chemistry': 70,
                 'English': 50,
                 'Mathematics': 80,
                 'Physics': 60}}

正如我們所看到的,學生數據結構的正常打印輸出和 pprint 函數的格式化輸出都打印在輸出中。我們可以觀察到正常輸出和 pprint()函數的格式化輸出之間的差異,並得出結論,pprint()函數的格式化輸出非常簡單且易於觀察。這就是我們如何在沒有任何參數的情況下使用 pprint()函數來打印給定數據結構的格式化輸出。

說明:我們首先將程序中的 pprint模塊導入為 ppr,之後定義了 PrettyPrinter 類的構造器。我們還定義了一些學生數據的示例數據結構,並將該數據結構命名為 students。我們將 PrettyPrinter 類的構造器變量命名為 ppConst,並在稍後與 pprint()函數一起使用。然後,我們將 pprint()函數與 ppConst 構造器變量一起使用。在使用 pprint()函數時,我們將學生的數據結構變量作為參數傳遞,以格式化學生數據結構的輸出。之後,首先,我們使用 studentDS 變量打印了數據結構的正常輸出。最後,我們使用 pprint()函數打印了 pprint()函數的格式化輸出,以便與正常輸出進行比較。

我們也可以使用 pprint()函數遞歸打印數據結構。在這裡,我們還將嘗試從給定的數據結構中打印一個遞歸對象,對於進一步的遞歸過程,在輸出中將只打印引用。查看下面的示例程序,了解 pprint()函數的遞歸對象打印實現:

示例 2: 看看下面的程序,我們已經在輸出中打印了一個遞歸對象:


# Importing the pprint module as ppr
import pprint as ppr
# Define a list of numbers
numList = list(range(24, 31))
# Appending the list
numList.append(numList)
# Printing appended number list
print("Appended list of numbers: ")
print(numList)
print("----------------")
#Printing recursive object with pprint() function
print("Appended list with recursive object from pprint function: ")
ppr.pprint(numList)

輸出:

Appended list of numbers: 
[24, 25, 26, 27, 28, 29, 30, [...]]
----------------
Appended list with recursive object from pprint function: 
[24, 25, 26, 27, 28, 29, 30, ] 

如我們所見,當我們使用 pprint()函數打印輸出中的附加列表時,列表中的最後一項被打印為遞歸對象。輸出顯示遞歸是從列表中最後一項的對象引用 id 開始的。

現在,我們將提供 PrettyPrinter 類的寬度參數(我們在上一節中討論過),同時為其定義構造器。我們在 PrettyPrinter()函數中提供的寬度根據給定的寬度格式化輸出,並以相同的格式呈現。查看下面的示例程序,以了解 PrettyPrinter 類的寬度參數實現:

示例 3: 看看下面的程序,我們在其中為 PrettyPrinter 類的構造器使用了寬度參數:


# Importing the pprint module as ppr
import pprint as ppr
# Defining a data structure which having student's data
studentDS = {"Dwayne Johnson": ["English", "Chemistry", "Physics", "Mathematics", "Biology"],
     "Randy Orton" :{"English": 50,"Physics": 60,
     "Chemistry": 70, "Mathematics": 80, "Biology": 90},
     "Karl Marx" :(50, 60, 70, 80, 90)}
# Defining Constructor for the PrettyPrinter class with width argument
ppConst = ppr.PrettyPrinter(width = 24)
# Printing formatted output of the pprint() function
print ("The pprint function's formatted output of the student data structure with defined width:")
ppConst.pprint(studentDS)

輸出:

The pprint function's formatted output of the student data structure with defined width:
{'Dwayne Johnson': ['English',
                    'Chemistry',
                    'Physics',
                    'Mathematics',
                    'Biology'],
 'Karl Marx': (50,
               60,
               70,
               80,
               90),
 'Randy Orton': {'Biology': 90,
                 'Chemistry': 70,
                 'English': 50,
                 'Mathematics': 80,
                 'Physics': 60}}

我們可以看到,學生數據結構的格式化輸出是根據我們在 PrettyPrinter 類中使用 width 參數提供的寬度打印的,即 width = 24。

在這個實現部分,我們將學習如何實現 pformat()函數來打印給定數據結構的格式化輸出。我們可以通過以下兩種方式從 PrettyPrinter 構造器中實現 pformat 函數:

(i) 作為方法的 pformat

(ii) 作為函數的 pformat

我們將在每種情況下使用一個示例程序來理解 pformat 的實現。

(i)使用 pformat 作為方法:

在這裡,我們將使用像 pformat 這樣的方法來打印帶有該功能的格式化輸出。我們可以看下面的示例程序來理解 pformat 的方法實現:


# Importing the pprint module as ppr
import pprint as ppr
# Defining a data structure which having student's data
studentDS = {"Dwayne Johnson": ["English", "Chemistry", "Physics", "Mathematics", "Biology"],
     "Randy Orton" :{"English": 50,"Physics": 60,
     "Chemistry": 70, "Mathematics": 80, "Biology": 90},
     "Karl Marx" :(50, 60, 70, 80, 90)}
# Defining Constructor for the PrettyPrinter class
ppConst = ppr.PrettyPrinter()
# Using pformat() function
pfOutput = ppConst.pformat(studentDS)
# Printing formatted output of the pprint() function
print ("The pformat method's formatted output of the student data structure:")
print(pfOutput)

輸出:

The pformat method's formatted output of the student data structure:
{'Dwayne Johnson': ['English',
                    'Chemistry',
                    'Physics',
                    'Mathematics',
                    'Biology'],
 'Karl Marx': (50, 60, 70, 80, 90),
 'Randy Orton': {'Biology': 90,
                 'Chemistry': 70,
                 'English': 50,
                 'Mathematics': 80,
                 'Physics': 60}}

(ii)使用 pformat 作為函數:

我們可以看下面的示例程序來理解 pformat 的函數實現:


# Importing the pprint module as ppr
import pprint as ppr
# Defining a data structure which having student's data
studentDS = {"Dwayne Johnson": ["English", "Chemistry", "Physics", "Mathematics", "Biology"],
     "Randy Orton" :{"English": 50,"Physics": 60,
     "Chemistry": 70, "Mathematics": 80, "Biology": 90},
     "Karl Marx" :(50, 60, 70, 80, 90)}
# Using pformat() function
pfOutput = ppr.pformat(studentDS)
# Printing formatted output of the pprint() function
print ("The pformat function's formatted output of the student data structure:")
print(pfOutput)

輸出:

The pformat function's formatted output of the student data structure:
{'Dwayne Johnson': ['English',
                    'Chemistry',
                    'Physics',
                    'Mathematics',
                    'Biology'],
 'Karl Marx': (50, 60, 70, 80, 90),
 'Randy Orton': {'Biology': 90,
                 'Chemistry': 70,
                 'English': 50,
                 'Mathematics': 80,
                 'Physics': 60}}

原創文章,作者:簡單一點,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/127270.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
簡單一點的頭像簡單一點
上一篇 2024-10-03 23:13
下一篇 2024-10-03 23:13

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python計算陽曆日期對應周幾

    本文介紹如何通過Python計算任意陽曆日期對應周幾。 一、獲取日期 獲取日期可以通過Python內置的模塊datetime實現,示例代碼如下: from datetime imp…

    編程 2025-04-29
  • 如何查看Anaconda中Python路徑

    對Anaconda中Python路徑即conda環境的查看進行詳細的闡述。 一、使用命令行查看 1、在Windows系統中,可以使用命令提示符(cmd)或者Anaconda Pro…

    編程 2025-04-29
  • Python中引入上一級目錄中函數

    Python中經常需要調用其他文件夾中的模塊或函數,其中一個常見的操作是引入上一級目錄中的函數。在此,我們將從多個角度詳細解釋如何在Python中引入上一級目錄的函數。 一、加入環…

    編程 2025-04-29
  • Python列表中負數的個數

    Python列表是一個有序的集合,可以存儲多個不同類型的元素。而負數是指小於0的整數。在Python列表中,我們想要找到負數的個數,可以通過以下幾個方面進行實現。 一、使用循環遍歷…

    編程 2025-04-29
  • Python清華鏡像下載

    Python清華鏡像是一個高質量的Python開發資源鏡像站,提供了Python及其相關的開發工具、框架和文檔的下載服務。本文將從以下幾個方面對Python清華鏡像下載進行詳細的闡…

    編程 2025-04-29
  • Python字典去重複工具

    使用Python語言編寫字典去重複工具,可幫助用戶快速去重複。 一、字典去重複工具的需求 在使用Python編寫程序時,我們經常需要處理數據文件,其中包含了大量的重複數據。為了方便…

    編程 2025-04-29
  • 蝴蝶優化算法Python版

    蝴蝶優化算法是一種基於仿生學的優化算法,模仿自然界中的蝴蝶進行搜索。它可以應用於多個領域的優化問題,包括數學優化、工程問題、機器學習等。本文將從多個方面對蝴蝶優化算法Python版…

    編程 2025-04-29
  • Python程序需要編譯才能執行

    Python 被廣泛應用於數據分析、人工智能、科學計算等領域,它的靈活性和簡單易學的性質使得越來越多的人喜歡使用 Python 進行編程。然而,在 Python 中程序執行的方式不…

    編程 2025-04-29
  • python強行終止程序快捷鍵

    本文將從多個方面對python強行終止程序快捷鍵進行詳細闡述,並提供相應代碼示例。 一、Ctrl+C快捷鍵 Ctrl+C快捷鍵是在終端中經常用來強行終止運行的程序。當你在終端中運行…

    編程 2025-04-29

發表回復

登錄後才能評論