繁体   English   中英

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

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

我有以下代码必须删除文本文件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)

这是问题,我不知道该放什么:

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

使用 string.index() 方法搜索要删除的笔记。 然后你可以使用 string.replace() 方法在它被删除后获取你的字符串

暂无
暂无

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

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