如何僅讀取 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.