我正在尝试整理以下文件
Fantasy
Supernatural
Fantasy
UrbanFantasy
Fantasy
EpicFantasy
Fantasy
HighFantasy
我想删除由本身出现的单词fantasy,并将新列表放入另一个文件中 我试过了
def getRidofFantasy():
file = open("FantasyGenres.txt", "r")
new_file = open("genres/fantasy", "w")
for line in file:
if line != "Fantasy":
new_file.write(line)
file.close()
new_file.close()
这不起作用,我不知道为什么。新文件与旧文件相同。有人能解释一下发生了什么,并给出一个正确解决方案的例子吗
试试这个
在代码中执行
for line in f
时line
变量还包括\n
(endline)字符,这就是它不起作用的原因与@Atin的答案类似,您也可以这样做:
试试这个
相关问题 更多 >
编程相关推荐