比较两个文件并写入一个新文件,但只输出几行?

时间:2013-08-29 15:23:44

标签: python file

我想比较两个txt文件和所有行,但是当我运行代码时它只响应了几个结果,循环没有效果运行,似乎错过了File1中的很多行,只出现了第一行File1中。

问题:

我的循环有问题吗?非常感谢!

def compareLines(filename1,filename2):

    File1=open(filename1,'r')
    File2=open(filename2,'r')
    File3=open("Result.txt",'w')

    finalList=[]
    for line1 in File1:
        for line2 in File2:
            set1=set(line1.split(" "))
            set2=set(line2.split(" "))
            print line1
            print line2
            similarNumber=len(set1.intersection(set2))/float(len(set1.union(set2)))
            File3.write('Simmilar rate:'+str(similarNumber)+' '+str(len(set1.intersection(set2)))+" words in incoindence\n")
            finalList.append(similarNumber)

    File1.close()
    File2.close()
    File3.close()

    os.remove(filename1)
    os.remove(filename2)

    return finalList

2 个答案:

答案 0 :(得分:2)

您应该在内循环中重新打开File2或使用file.seek跳转到文件的开头,因为在第一次迭代后,文件指针位于File2的末尾。

file.seek的帮助:

>>> print file.seek.__doc__
seek(offset[, whence]) -> None.  Move to new file position.

Argument offset is a byte count.  Optional argument whence defaults to
0 (offset from start of file, offset should be >= 0); other values are 1
(move relative to current position, positive or negative), and 2 (move
relative to end of file, usually negative,....

答案 1 :(得分:0)

感谢上面的答案。我特别感谢Nisan.H和Ashwini Chaudhary,我已经通过你的帮助解决了这个问题,在"之前只添加一行,用于File2中的第2行" ~~~

def compareLines(filename1,filename2):

File1=open(filename1,'r')
File2=open(filename2,'r')
File3=open("Result.txt",'w')

finalList=[]

for line1 in File1:
    File2.seek(0)
    for line2 in File2:
        set1=set(line1.split(" "))
        set2=set(line2.split(" "))
        print line1
        print line2
        similarNumber=len(set1.intersection(set2))/float(len(set1.union(set2)))
        File3.write('Simmilar rate:'+str(similarNumber)+' '+str(len(set1.intersection(set2)))+" words in incoindence\n")
        finalList.append(similarNumber)

File1.close()
File2.close()
File3.close()

os.remove(filename1)
os.remove(filename2)

return finalList