因此,我对Python相当陌生。在阅读了一些不同的教程之后,我决定尝试制作一个简单的程序,其中的一个功能是删除txt文件中的一行。以下是我目前拥有的代码:
name = raw_input("What name would you like to remove: ")
templist = open("oplist.txt").readlines()
templist_index = templist.index(name)
templist.remove(templist_index)
target = open("oplist.txt", "w")
target.write(templist)
target.close
但是,当创建templist时,它会存储“example1\n”之类的数据,如果用户只键入example,它将无法工作。有没有更简单的方法来解决这个问题?谢谢你的帮助
使用
rstrip
删除换行符并使用with
打开文件:您还可以将换行符命名为:
完整代码:
相关问题 更多 >
编程相关推荐