如何用 Python 只讀取檔案的首行?


在 Python 中,檔案處理是一個涵蓋數十個操作的大主題;其中一個操作是讀取檔案的首行,這允許您快速訪問和檢索特定資訊,而無需讀取整個檔案。在本文中,我們將研究幾種使用 Python 只讀取檔案首行的方法。您可以參考以下清晰明瞭的解釋和程式碼示例來理解和掌握讀取檔案首行的過程。

使用 readline() 方法

要讀取檔案的首行,請按照以下步驟操作:

  • 步驟 1:使用 open() 函式以讀取模式開啟檔案。

  • 步驟 2:然後將返回的檔案物件賦值給一個變數。

  • 步驟 3:使用 readline() 方法讀取檔案的首行並將其儲存在一個變數中。

  • 步驟 4:接下來處理檔案的首行(例如,列印它或執行其他操作)。

  • 步驟 5:最後,關閉檔案以確保正確處理系統資源。

以下是展示該過程的程式碼片段:

示例

假設我們有一個名為 myfile.txt 的檔案,其內容如下:

#myfile.txt
This is a test file

# Open the file for reading
file = open('myfile.txt', 'r')

# Read the first line of the file
first_line = file.readline()

# Process the first line (e.g., print it)
print("First line:", first_line)

# Close the file
file.close()

執行上述程式碼,並開啟myfile.txt後,我們將看到以下輸出:

輸出

First line: This is a test file

使用迴圈讀取首行

作為另一種方法,您可以使用迴圈來讀取檔案的首行。儘管這種方法效率不高,但它仍然允許您處理首行可能包含特殊字元或需要額外處理的不同情況。

讓我們考慮以下程式碼片段:

示例

假設我們有一個名為 myfile.txt 的檔案,其內容如下:

#myfile.txt
This is a test file

# Open the file for reading
file = open('myfile.txt', 'r')

# Read the first line of the file using a loop
for line in file:
    first_line = line
    break

# Process the first line (e.g., print it)
print("First line:", first_line)

# Close the file
file.close()

執行上述程式碼,並開啟myfile.txt後,我們將看到以下輸出:

輸出

First line: This is a test file

使用上下文管理器讀取首行

我們已經瞭解到,Python 提供了一種使用上下文管理器方便地讀取檔案的方法。以下是如何使用上下文管理器讀取檔案首行的方法:

  • 步驟 1:使用“with 語句”和 open() 函式以讀取模式開啟檔案。

  • 步驟 2:接下來,在 with 塊內將返回的檔案物件賦值給一個變數。

  • 步驟 3:使用 readline() 方法讀取檔案的首行並將其儲存在一個變數中。

  • 步驟 4:最後,處理首行(例如,列印它或執行其他操作)。

讓我們來看一個如下所示的程式碼片段:

示例

假設我們有一個名為 myfile.txt 的檔案,其內容如下:

#myfile.txt
This is a test file

# Open the file for reading using a context manager
with open('myfile.txt', 'r') as file:
    # Read the first line of the file
    first_line = file.readline()

    # Process the first line (e.g., print it)
    print("First line:", first_line)

執行上述程式碼,並開啟myfile.txt後,我們將看到以下輸出:

輸出

First line: This is a test file

使用 next() 函式讀取首行

要讀取檔案的首行,您還有另一種方法;您也可以將 next() 函式與 open() 函式結合使用。這種方法使您可以直接訪問首行,而無需使用“for 迴圈”。請按照以下步驟操作:

  • 步驟 1:使用 open() 函式以讀取模式開啟檔案。

  • 步驟 2:將返回的檔案物件賦值給一個變數。

  • 步驟 3:使用 next() 函式並將檔案物件作為引數來檢索首行。

  • 步驟 4:處理首行(例如,列印它或執行其他操作)。

  • 步驟 5:最後,關閉檔案以確保正確處理系統資源。

讓我們考慮以下程式碼片段:

示例

假設我們有一個名為 myfile.txt 的檔案,其內容如下:

#myfile.txt
This is a test file

# Open the file for reading
file = open('myfile.txt', 'r')

# Get the first line of the file using the next() function
first_line = next(file)

# Process the first line (e.g., print it)
print("First line:", first_line)

# Close the file
file.close()

執行上述程式碼,並開啟myfile.txt後,我們將看到以下輸出:

輸出

First line: This is a test file

讀取並去除首行的空格

有時會發現,檔案的首行可能包含前導或尾隨空格字元。要刪除此類空格字元,您可以使用 strip() 方法。讓我們來看一個程式碼片段:

示例

在這裡,我們使用 readline() 方法讀取檔案的首行,並立即應用 strip() 方法來刪除任何前導或尾隨空格字元。這確保了處理後的首行不包含任何不必要的空格。

假設我們有一個名為 myfile.txt 的檔案,其內容如下:

#myfile.txt
This is a test file

# Open the file for reading
file = open('myfile.txt', 'r')
# Read the first line of the file
first_line = file.readline().strip()

# Process the first line (e.g., print it)
print("First line:", first_line)

# Close the file
file.close()

執行上述程式碼,並開啟myfile.txt後,我們將看到以下輸出:

輸出

First line: This is a test file

到目前為止,您一定已經瞭解到,讀取檔案的首行操作是 Python 檔案處理中非常常規且常見的一種操作。在本文中,我們透過一些示例說明了如何使用不同的方法來讀取檔案的首行。我們還提供了其他示例,說明了如何使用 next() 函式讀取首行以及如何去除首行中的任何前導或尾隨空格字元。透過遵循清晰明瞭的解釋和程式碼片段,您一定已經對在 Python 中讀取檔案首行有了很好的理解。最好記住在讀取檔案後關閉檔案,以確保正確處理系統資源。

更新於:2023年7月13日

14K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.