txt1-txt2去重输出到txt3

 1 # -*- coding: utf-8 -*-
 2 # python 2.7
 3 import sys
 4 reload(sys)
 5 sys.setdefaultencoding( "utf-8" )
 6 # 本方法用于对txt1中含有的txt2内容进行去重,并输出到txt3
 7 # 由于文件输出,写入过程中有不可见编码,所以用strip()进行扫尾
 8 def txt_quchong(txt1path,txt2path,txt3path):
 9 
10     fp1 = file(txt1path, 'r')
11     fp2 = file(txt2path, 'r')
12     fp3 = file(txt3path, 'w')
13     d1 = {}
14     d2 = {}
15     isFirst = True
16     for line in fp1:
17         line=line.strip()
18         if not isFirst:
19             d1[hash(line)] = line
20         else:
21             isFirst = False
22     fp1.close()
23     isFirst = True
24     for line in fp2:
25         line=line.strip()
26         if not isFirst:
27             d2[hash(line)] = line
28         else:
29             isFirst = False
30     fp2.close()
31     diff = set(d1.keys()) - set(d2.keys())
32     for key in diff:
33         fp3.write(d1[key]+"
")
34     fp3.close()
原文地址:https://www.cnblogs.com/oneby/p/5454188.html