我尝试删除两个重复项,如:
STANGHOLMEN_TA02_GT11
STANGHOLMEN_TA02_GT41
STANGHOLMEN_TA02_GT81
STANGHOLMEN_TA02_GT11
STANGHOLMEN_TA02_GT81
结果
STANGHOLMEN_TA02_GT41
我试过这个剧本
lines_seen = set()
with open(example.txt, "w") as output_file:
for each_line in open(example2.txt, "r"):
if each_line not in lines_seen:
output_file.write(each_line)
lines_seen.add(each_line)
但不幸的是,它不能像我所希望的那样工作,它会漏掉行,也不会删除行。原始文件的行之间不时有空格
你需要做两次才能让它正常工作。因为通过1次,您将不知道当前行稍后是否会重复。您应该尝试以下方法:
相关问题 更多 >
编程相关推荐