使用python从文本文件中删除两个副本(原始和重复)

2024-10-01 22:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试删除两个重复项,如:

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)

但不幸的是,它不能像我所希望的那样工作,它会漏掉行,也不会删除行。原始文件的行之间不时有空格


Tags: intxtoutputlineopenfileeachlines
1条回答
网友
1楼 · 发布于 2024-10-01 22:33:27

你需要做两次才能让它正常工作。因为通过1次,您将不知道当前行稍后是否会重复。您应该尝试以下方法:

# count each line occurances
lines_count = {}
for each_line in open('example2.txt', "r"):
    lines_count[each_line] = lines_count.get(each_line, 0) + 1

# write only the lines that are not repeated
with open('example.txt', "w") as output_file:
    for each_line, count in lines_count.items():
        if count == 1:
            output_file.write(each_line)

相关问题 更多 >

    热门问题