发现差异是行不通的

时间:2015-06-18 07:20:55

标签: python difference array-difference

我试图找到两个文件的区别,但我仍然从两个文件中得到答案

这是我的代码

#File one(This file contents should be removed after comparing file two)
a = open('diff1','r+')
#File two
#This file has 999999 records
b = open('gtin','r+')
f8 = open('missing-test-remove.txt','w')
def diff(a, b):
    c = set(a).union(set(b))
    d = set(a).intersection(set(b))
    result =  list(c - d)
    for s in result:
        print s
        f8.write(s)

diff(a,b)

但是我仍然从两个文件中得到相同的结果,但是在与文件二进行比较后应删除文件一个内容

2 个答案:

答案 0 :(得分:1)

你做错了是 -

c = set(a).union(set(b))
d = set(a).intersection(set(b))

请注意ab仍为文件描述符,一旦执行set(a),如果再次执行set(a),您将获得一个空集,因为在首先调用set(a),已经读取了完整的文件,文件的光标就在最后。

您需要更改代码,以便只调用set(a)和`set(b)一次。一些东西 -

#File one(This file contents should be removed after comparing file two)
a = open('diff1','r+')
#File two
#This file has 999999 records
b = open('gtin','r+')
f8 = open('missing-test-remove.txt','w')
def diff(a, b):
    sa = set(a)
    sb = set(b)
    c = sa.union(sb)
    d = sa.intersection(sb)
    result =  list(c - d)
    for s in result:
        print s
        f8.write(s)

diff(a,b)

此外,您应该在完成写入后刷新您编写的文件,最后关闭所有文件 -

a.close()
b.close()
f8.close()

答案 1 :(得分:0)

您需要保存设定值。一个简单的测试:

    print a
    print set(a)
    print a
    print set(a) # wrong

所以

    seta = set(a)
    setb = set(b)
    setc = seta.union(setb)
    setd = seta.intersection(setb)