如何比较两个文件只得到不在第二个文件中的行?

2024-05-26 16:27:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个这样的文件:

file 1 :                     file 2 :
col1    col2                 col1     col2
john    kerry                john     kerry
adam    lord                 bob      abram  
joe     hitch               

我想根据姓氏和名字比较这两个文件,只得到一个不包含文件2中人员的文件,也就是说:

^{pr2}$

我试过了,但没有得到正确的输出

^{3}$

Tags: 文件人员名字johnfilecol2col1bob
3条回答

如果文件格式相同,我认为您不需要csv模块。 这个解决方案怎么样:

exclude_names = frozenset(open('file2')) # make set for performance
with open('output', 'w') as f:
    for name in open('file1'):
        if name not in exclude_names:
             f.write(name)

使用csv读写器的解决方案:

^{pr2}$
results=[i for i, j in zip(reader1, reader2) if i != j]

如果顺序不重要,则使用set(reader1) - set(reader2)。在

^{pr2}$

我会用一个固定的差异:

with open('file1') as f1, open('file2') as f2:
    data1 = set(f1)
    lines_not_in_f2 = data1.difference(f2)

如果文件的格式可能略有不同,则可能需要将文件对象包装在生成元组的生成器中:

^{pr2}$

这样做的好处是不需要将整个f2文件读入内存。它的缺点是输出名称是无序的(因为它们存储在一个集合中)。在

相关问题 更多 >

    热门问题