简体   繁体   English

我希望能够删除此文本文件中的特定行

[英]I want to be able to delete a specific line in this text file

I have the following code which must delete a specific line in the text file notes.txt :我有以下代码必须删除文本文件notes.txt的特定行:

f = open("notes.txt","r+")

notestext = f.read()

print("NOTES:")

print(notestext)

enter = input("Press enter to add or change notes \n")

print("Type delete to delete a note or add to add a note")

start = input()
if start == "add":
    notetoadd = input("Type your note to add \n")
    f.write("\n")
    f.write(notetoadd)

Here is the problem, i dunno what to put here:这是问题,我不知道该放什么:

if start == "delete":
    notetodelete = input("Type the note you want to delete \n")
f.close()

Search for the note you want to delete using the string.index() method.使用 string.index() 方法搜索要删除的笔记。 You can then use string.replace() method to get your string after it gets deleted然后你可以使用 string.replace() 方法在它被删除后获取你的字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM