----> 22.txt <---- 111,aaa 222,bbb 222,bbb ----> 33.txt <---- 111,aaa 333,bbb #取交集 intersection def compare_txt_inter(file1,file2): with open(file1,"r") as file1, open(file2,"r") as file2: content1=set(file1.readlines()) #set返回一个无序去重的集合 eg:{1,3,2} content2=set(file2.readlines()) content3=content1.intersection(content2) return content3 #返回结果 #111,aaa #取并集 union def compare_txt_union(file1,file2): with open(file1,"r") as file1, open(file2,"r") as file2: content1=set(file1.readlines()) content2=set(file2.readlines()) content3=content1.union(content2) return content3 #返回结果 #111,aaa #222,bbb #333,bbb #取差集 symmetric_difference def compare_txt_diff(file1,file2): with open(file1,"r") as file1, open(file2,"r") as file2: content1=set(file1.readlines()) content2=set(file2.readlines()) content3=content1.symmetric_difference(content2) return content3 #返回结果 #222,bbb #333,bbb file1='D:\\Test_Script\\22.txt' file2='D:\\Test_Script\\33.txt' content=compare_txt_diff(file1,file2) if content: with open('D:\\Test_Script\\44.txt','w') as outfile: for line in content: outfile.write(line) else: print("内容为空")
标签:22,python,----,33,文档,txt From: https://www.cnblogs.com/yi-ying/p/17580548.html