1

我有两个文件,我在获取新行时没有任何问题,new filenew_fileold_file. 我得到所有的线。我怎样才能得到删除的线?

import difflib
old_file=open(OUTPUT).readlines()
new_file=open(TMP_FILE).readlines()

diff = difflib.unified_diff(old_file, new_file, fromfile='file1', tofile='file2', lineterm='', n=0)
lines = list(diff)[2:]
added = [line[1:] for line in lines if line[0] == '+']
removed = [line[1:] for line in lines if line[0] == '-']

added_users=[]
for line in added:
    if line not in removed:
        added_users.append(line)
4

1 回答 1

-1

我建议您使用commto diff 文件。

NAME
   comm - compare two sorted files line by line

SYNOPSIS
   comm [OPTION]... FILE1 FILE2

DESCRIPTION
   Compare sorted files FILE1 and FILE2 line by line.

   With no options, produce three-column output. 
   Column one contains lines unique to FILE1,
   column two contains lines unique to FILE2, 
   and column three contains lines common to both files.

   -1     suppress lines unique to FILE1

   -2     suppress lines unique to FILE2

   -3     suppress lines that appear in both files

所以你可以做以下

comm -1 old new > removed
comm -2 old new > added 

输入文件必须排序。如果不是,请先对它们进行排序。

comm -1 <(sort old) <sort(new) > removed
于 2015-08-26T08:30:34.163 回答