如何在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()
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
输出
执行上述程序将生成以下输出:
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,即起始行号。我们遍历文件直到到达末尾,然后检查用户输入的数字是否等于要删除的行号。如果为假,则我们不需要删除或移除该行,因此我们将其写入文件。我们不是删除指定的行,而是将剩余的行添加到文件中,因此删除的行不会出现在结果文件中。对于每一行,行号的值都会增加一。
在本文中,我们学习了如何从文本文件中删除特定行并将剩余的行保存在同一文件中。除此之外,我们还学习了如何从头到尾遍历整个文本文件,以及如何读取和写入文本文件中的数据。