如何在Python中刪除文字檔案中的特定行?


在本文中,我們將向您展示如何使用python刪除文字檔案中的特定行。

假設我們有一個名為**TextFile.txt**的文字檔案,其中包含一些隨機文字。我們將從文字檔案中刪除特定行(例如第2行)。

TextFile.txt

Good Morning
This is Tutorials Point sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome everyone
Learn with a joy

演算法

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存文字檔案的路徑。

  • 使用**open()**函式(開啟檔案並返回檔案物件作為結果)以只讀模式開啟文字檔案,將檔名和模式作為引數傳遞給它(此處**“r”**表示只讀模式)。

with open(inputFile, 'r') as filedata:
  • 使用**readlines()**函式(返回一個列表,其中檔案的每一行都表示為列表項。要限制返回的行數,請使用hint引數。如果返回的位元組總數超過指定數量,則不再返回更多行)來獲取給定輸入文字檔案的行列表。

file.readlines(hint)
  • 建立一個變數(儲存行號)並將它的值初始化為1。

  • 使用**open()**函式(開啟檔案並返回檔案物件作為結果)以寫入模式開啟給定的文字檔案,將檔名和模式作為引數傳遞給它(此處**“w”**表示寫入模式)。

with open(inputFile, 'w') as filedata:
  • 使用for迴圈遍歷檔案的每一行。

  • 使用**input()**函式(input()函式從輸入(使用者)讀取一行,透過消除尾隨換行符將其轉換為字串,並返回它。遇到EOF時,會丟擲EOFError異常)輸入要刪除的行號,並使用**int()**函式(轉換為整數)將其型別轉換為整數。

  • 使用if條件語句確定行索引(行號)是否不等於給定的刪除行號。

  • 如果條件為真,則使用**write()**函式(將指定的文字寫入檔案。提供的文字將根據檔案模式和流位置插入)將對應的行寫入檔案。

  • 將行索引(行號)的值增加1。

  • 如果成功刪除給定的特定行,則列印一些隨機文字。

  • 使用open()函式再次以讀取模式開啟輸入檔案,以列印刪除給定特定行後的檔案內容。

  • 使用for迴圈遍歷檔案的每一行。

  • 列印文字檔案的每一行。

  • 使用**close()**函式(用於關閉開啟的檔案)關閉輸入檔案。

示例

以下程式用於從文字檔案中刪除給定行並在刪除該行後列印結果檔案內容:

# input text file inputFile = "TextFile.txt" # Opening the given file in read-only mode. with open(inputFile, 'r') as filedata: # Read the file lines using readlines() inputFilelines = filedata.readlines() # storing the current line number in a variable lineindex = 1 # Enter the line number to be deleted deleteLine = int(input("Enter the line number to be deleted = ")) # Opening the given file in write mode. with open(inputFile, 'w') as filedata: # Traverse in each line of the file for textline in inputFilelines: # Checking whether the line index(line number) is # not equal to a given delete line number if lineindex != deleteLine: # If it is true, then write that corresponding line into file filedata.write(textline) # Increase the value of line index(line number) value by 1 lineindex += 1 # Print some random text if the given particular line is deleted successfully print("Line",deleteLine,'is deleted successfully\n') # Printing the file content after deleting the specific line print("File Content After Deletion :") # Reading the file again in read mode givenFile = open(inputFile,"r") # Traversing the file line by line for line in givenFile: # printing each line print(line) # Closing the input file filedata.close()

輸出

執行上述程式將生成以下輸出:

Enter the line number to be deleted = 2
Line 2 is deleted successfully

File Content After Deletion :
Good Morning
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome everyone
Learn with a joy

我們向程式提供了一個包含一些隨機內容的文字檔案,然後以讀取模式開啟它。我們建立了一個變數來儲存當前行號並將其初始化為1,即起始行號。我們遍歷檔案直到到達末尾,然後檢查使用者輸入的數字是否等於要刪除的行號。如果為假,則我們不需要刪除或移除該行,因此我們將其寫入檔案。我們不是刪除指定的行,而是將剩餘的行新增到檔案中,因此刪除的行不會出現在結果檔案中。對於每一行,行號的值都會增加一。

在本文中,我們學習瞭如何從文字檔案中刪除特定行並將剩餘的行儲存在同一檔案中。除此之外,我們還學習瞭如何從頭到尾遍歷整個文字檔案,以及如何讀取和寫入文字檔案中的資料。

更新於:2022年8月1日

24K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

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