从两个文件python中删除行

时间:2015-08-26 08:18:12

标签: python diff difflib

我有两个文件,我在new file获取新行时没有任何问题,但与new_file相比,我无法在old_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)

0 个答案:

没有答案