0

我有两个文件, file1 包含的内容为

啊啊啊

bbb

ccc

文件 2 包含的内容为

ccc

ddd

eee

啊啊啊

rrr

bbb

nnn

我想这样做,如果 file2 包含 file1 的行,那么该行将从 file2 中删除。最后,file2 会变成 ddd eee rrr nnn 另外,我的代码是

f1 = open("test1.txt","r")
f2 = open("test2.txt","r")

    fileOne = f1.readlines()
    fileTwo = f2.readlines()
    f1.close()
    f2.close()
    outFile = open("test.txt","w")
    x = 0
    for i in fileOne:
        if i !=  fileTwo[x]:
            outFile.writelines(fileTwo[x])
        x += 1

outFile.close()

谢谢你。

4

3 回答 3

4
with open("f1.txt") as f1:
    s1 = set(f1)
with open("f2.txt") as f2, open("f3.txt","w") as f3:
    f3.writelines(x for x in f2 if x not in s1)

使用上下文管理器关闭文件是一种很好的做法(这就是这样with做的)。

set检查 a 的成员资格比检查 a 的效率要高得多list

如果可能有额外的空白,你应该像这样去掉这些行

with open("f1.txt") as f1:
    s1 = set(x.strip() for x in f1)
with open("f2.txt") as f2, open("f3.txt","w") as f3:
    f3.writelines(x for x in f2 if x.strip() not in s1)
于 2013-05-15T16:45:38.517 回答
0

使用 set Difference 来查找两个文件的差异。

f1 = open("test1.txt","r").readlines()
f2 = open("test2.txt","r").readlines()

diff = set(f2) - set(f1)
outFile = open("test.txt","w")
outFile.writelines(line for line in f2 if line in diff)
于 2013-05-15T16:35:07.120 回答
0

使用您的代码...

f1 = open("test1.txt","r").read()
f2 = open("test2.txt","r").read()

fileOne = f1.splitlines()
fileTwo = f2.splitlines()

# remove the dup lines
nodup_lines = [line for line in fileTwo if line not in fileOne]
# join using newline character
newFileTwo = '\n'.join(nodup_lines)

# write file
outFile = open("test.txt","w")
outFile.write(newFileTwo)
outFile.close()
于 2013-05-15T16:38:09.103 回答