Python如何在文本文件中查找唯一元素并输出到另一个文本文件

2 投票
4 回答
8630 浏览
提问于 2025-04-18 03:53

我有一个叫做sample.txt的文本文件,里面的内容是:

abc  
abc
egf
abc 
xyz
efg
abc
xyz
efg
xyz
xyz

我想找出这些内容中独特的元素,并把它们存储到另一个文本文件output.txt里。

我希望得到的结果是:

abc
efg
xyz
egf

因为我对Python和Stackoverflow都很陌生,有人能从头开始帮我吗?谢谢!

4 个回答

0

这个程序只使用了列表和简单的文件操作。

element = raw_input("Element you want to find : ")
with open('sample.txt','r') as fp:
    listp = fp.readlines()
modifiedlist =[]
for i in range(0,len(listp)):
    word = listp[i]
    modifiedlist.append(word[0: len(word)-1])
print modifiedlist

fi = open('input.txt','w')
if element in modifiedlist:
    print element + "is present"
    fi.write(element)
fi.close()
2

试试这个,

try:
    with open(r'd:\text.txt') as f:
        l=[i.rstrip() for i in f]
    l=set(l)
    f1=open(r'd:\out.txt',"w")
    for i in l:
        f1.write(i)
        f1.write('\n')
    f.close()
    f1.close()
    print 'Done'
except Exception as e:
    print 'Error' , e
8

如果你在一个类似Unix的系统上工作,其实不需要写一个Python脚本来完成这个任务。使用管道和过滤器就可以了:

$ cat sample.txt | sort | uniq > output.txt

正如@devnull提到的,这个可以写得更简洁一些:

$ sort sample.txt | uniq > output.txt

如果你真的想用Python来做:

seen = set()
with open('sample.txt') as infile:
    with open('output.txt', 'w') as outfile:
        for line in infile:
            if line not in seen:
                outfile.write(line)
                seen.add(line)

这个代码会按照第一次出现的顺序打印出唯一的行。

还有一种更简洁的方法是使用collections.OrderedDict和多上下文管理器的形式来使用with语句

from collections import OrderedDict

with open('sample.txt') as infile, open('output.txt', 'w') as outfile:
    outfile.writelines(OrderedDict.fromkeys(infile))  
0

假设输出的顺序并不重要

with open('input_file','r') as f:                                                                                                                                                                                                                                                 
    distinct_content=set(f.readlines())                                                                                                                                                                                                                                                   

to_file=""                                                                                                                                                                                                                                                                       
for element in distinct_content:                                                                                                                                                                                                                                                               
    to_file=to_file+element                                                                                                                                                                                                                                                           
with open('output_file','w') as w:                                                                                                                                                                                                                                                  
    w.write(to_file) 

输出结果:

efg
egf
xyz
abc

撰写回答