python中实现两个txt文档的去重功能(即去掉第一个文档中和第二文档相同的行)

2024-12-23 03:25:37
推荐回答(1个)
回答1:

# -*- coding: utf-8 -*-
#
# python 2.7

fp1 = file('', 'r')
fp2 = file('', 'r')
fp3 = file('', 'w')

d1 = {}
d2 = {}

isFirst = True

for line in fp1:
if not isFirst:
d1[hash(line)] = line
else:
isFirst = False

fp1.close()

isFirst = True
for line in fp2:
if not isFirst:
d2[hash(line)] = line
else:
isFirst = False

fp2.close()

diff = set(d1.keys()) - set(d2.keys())
for key in diff:
fp3.write(d1[key])
fp3.close()