码迷,mamicode.com
首页 > 其他好文 > 详细

txt1-txt2去重输出到txt3

时间:2016-05-03 12:27:25      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

 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]+"\r\n")
34     fp3.close()

 

txt1-txt2去重输出到txt3

标签:

原文地址:http://www.cnblogs.com/oneby/p/5454188.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!