使用Python从文本文件中删除一行

使用Python从文本文件中删除一行

本文介绍了使用Python从文本文件中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件,每行以数字开头。用户可以通过输入用户想要删除的行号删除一行。



我遇到的问题是设置打开它的模式。当我使用 a + 时,原始内容仍然存在。但是,在文件末尾加上了我想保留的行。另一方面,当我使用 w + 时,整个文件被删除。我敢肯定有一个比 w + 模式更好的方法,删除所有内容,然后重新打开它并添加行。

  def DeleteToDo(self):
print您要删除哪个项目?
DeleteItem = raw_input(>)#选择要删除的行号
打印您确定要删除数字+ DeleteItem +(y / n)
VerifyDelete = str.lower(raw_input(>))
如果VerifyDelete ==y:
FILE = open(ToDo.filename,a +)#打开文件,整个文件被删除)
FileLines = FILE.readlines()#读取并显示行
在FileLines中的行:
FILE.truncate()
如果行[0: 1]!= DeleteItem:#如果当前行的数字(第一个字符)不等于要删除的数字,则重新写入该行
FILE.write(line)
else:
printNothing Deleted

这是一个典型的文件可能看起来像

1。 info here
2.更多的东西在这里
3.更多的东西在这里


print(你想要删除哪个项目?)
DeleteItem = raw_input( >)#选择行号删除
print(您确定要删除数字+ DeleteItem +(y / n))
DeleteItem = int(DeleteItem)
VerifyDelete = str.lower(raw_input(>))
如果VerifyDelete ==y:
FILE = open('data.txt',r)#打开文件(尝试w +以及整个文件被删除)
lines = [x.strip()for x in FILE if int(x [:x.index('。')]!= DeleteItem] #read all除了与要删除的行号相匹配的行之外的第一行
FILE.close()
FILE = open('data.txt',w)#再次打开文件
(x +'\ n')#将数据写入文件

else:
print(Nothing Deleted)
DeleteToDo()


I have a file where each line starts with a number. The user can delete a row by typing in the number of the row the user would like to delete.

The issue I'm having is setting the mode for opening it. When I use a+, the original content is still there. However, tacked onto the end of the file are the lines that I want to keep. On the other hand, when I use w+, the entire file is deleted. I'm sure there is a better way than opening it with w+ mode, deleting everything, and then re-opening it and appending the lines.

 def DeleteToDo(self):
    print "Which Item Do You Want To Delete?"
    DeleteItem = raw_input(">") #select a line number to delete
    print "Are You Sure You Want To Delete Number" + DeleteItem + "(y/n)"
    VerifyDelete = str.lower(raw_input(">"))
    if VerifyDelete == "y":
        FILE = open(ToDo.filename,"a+") #open the file (tried w+ as well, entire file is deleted)
        FileLines = FILE.readlines() #read and display the lines
        for line in FileLines:
            FILE.truncate()
            if line[0:1] != DeleteItem: #if the number (first character) of the current line doesn't equal the number to be deleted, re-write that line
                FILE.write(line)
    else:
        print "Nothing Deleted"

This is what a typical file may look like

1. info here
2. more stuff here
3. even more stuff here
解决方案
def DeleteToDo():
    print ("Which Item Do You Want To Delete?")
    DeleteItem = raw_input(">") #select a line number to delete
    print ("Are You Sure You Want To Delete Number" + DeleteItem + "(y/n)")
    DeleteItem=int(DeleteItem)
    VerifyDelete = str.lower(raw_input(">"))
    if VerifyDelete == "y":
        FILE = open('data.txt',"r") #open the file (tried w+ as well, entire file is deleted)
        lines=[x.strip() for x in FILE if int(x[:x.index('.')])!=DeleteItem] #read all the lines first except the line which matches the line number to be deleted
        FILE.close()
        FILE = open('data.txt',"w")#open the file again
        for x in lines:FILE.write(x+'\n')    #write the data to the file

    else:
        print ("Nothing Deleted")
DeleteToDo()

这篇关于使用Python从文本文件中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:58