python difflib比较文件

标签 python text difflib

我正在尝试使用 difflib 为两个包含推文的文本文件生成差异。代码如下:

#!/usr/bin/env python

# difflib_test

import difflib

file1 = open('/home/saad/Code/test/new_tweets', 'r')
file2 = open('/home/saad/PTITVProgs', 'r')

diff = difflib.context_diff(file1.readlines(), file2.readlines())
delta = ''.join(diff)
print delta

这是 PTITVProgs 文本文件:

Watch PTI on April 6th (7) Dr Israr Shah at 10PM on Business Plus in "Talking Policy". Rgds #PTI
CORRECTION!! Watch PTI on April 6th (5) @Asad_Umar  at 8PM on ARY News. Rgds #PTI
Watch PTI on April 6th (5) @Asad_Umar  at 8PM on AAJ News (6) PTI vs PMLN at 8PM on NewsOne. Rgds #PTI
Watch PTI on April 6th (5) Asad Umar at 8PM on AAJ News (6) PTI vs PMLN at 8PM on NewsOne. Rgds #PTI
Watch PTI on April 6th (5) Waleed Iqbal at 8PM on Channel 5. Rgds #PTI
Watch PTI on April 6th (3) Dr Israr Shah at 10PM on PTV News. Rgds #PTI
Watch PTI on April 6th (4) Javed hashmi at 1PM on PTV News. Rgds #PTI
Watch PTI on April 6th (3) Imran Alvi at 1PM on AAJ News. Rgds #PTI
Watch PTI on April 6th (1) Dr @ArifAlvi, Andleeb Abbas and Ehtisham Ameer at 11PM on ARY News (2) Hamid Khan at 10PM on ATV. Rgds #PTI
Watch PTI on April 5th (1) Farooq Amjad Meer at 10:45PM on Dunya News. Rgds #PTI
Watch PTI on April 4th (4) Faisal Khan at 8PM on PTV News. Rgds #PTI
@FaisalJavedKhan
Watch PTI on April 4th (3) Faisal Khan at 11PM on ATV. Rgds #PTI
@FaisalJavedKhan
Watch PTI on April 4th (1) Dr Israr Shah at 8PM on Waqt News (2) Dr Arif Alvi at 9PM on PTV World. Rgds #PTI
@ArifAlvi
Watch PTI on April 3rd (12) Abrar ul Haq on 10PM on Dawn News (13) Shabbir Sial at 10PM on Channel5. Rgds #PTI
Watch PTI on April 3rd (11) Sadaqat Abbasi on 8PM on RohiTV. Rgds #PTI
Watch PTI on April 3rd (10) Dr Zarqa and Andleeb Abbas on 8PM on Waqt News. Rgds #PTI
Watch PTI on April 3rd (9) Fauzia Kasuri at 8PM on Din News. Rgds #PTI
Watch PTI on April 3rd (8) Mehmood Rasheed at 8PM on ARY News. Rgds #PTI
Watch PTI on April 3rd (7) Israr Abbasi (Repeat on Arp 4th) at 1:20AM and 1PM on Vibe TV. Rgds #PTI
Watch PTI on April 3rd (5) Rao Fahad at 9PM on Express News (6) Dr Seems Zia at 10:30PM on Health TV. Rgds #PTI

这是 new_tweets 文本文件:

Watch PTI on April 7th (3) Malaika Reza at 8PM on AAJ News (4) Shah Mehmood Qureshi at 8PM on Geo News. Rgds #PTI
Watch PTI on April 7th (2) Chairman IMRAN KHAN at 10PM on PTV News in News Night with Sadia Afzal, Rpt: 2AM, 2PM. Rgds #PTI
@ImranKhanPTI
Watch PTI on April 7th (1) Dr Waseem Shahzad NOW at 6PM on PTV News. Rgds #PTI
Watch PTI on April 6th (7) Dr Israr Shah at 10PM on Business Plus in "Talking Policy". Rgds #PTI
CORRECTION!! Watch PTI on April 6th (5) @Asad_Umar  at 8PM on ARY News. Rgds #PTI
Watch PTI on April 6th (5) @Asad_Umar  at 8PM on AAJ News (6) PTI vs PMLN at 8PM on NewsOne. Rgds #PTI
Watch PTI on April 6th (5) Asad Umar at 8PM on AAJ News (6) PTI vs PMLN at 8PM on NewsOne. Rgds #PTI
Watch PTI on April 6th (5) Waleed Iqbal at 8PM on Channel 5. Rgds #PTI
Watch PTI on April 6th (3) Dr Israr Shah at 10PM on PTV News. Rgds #PTI
Watch PTI on April 6th (4) Javed hashmi at 1PM on PTV News. Rgds #PTI
Watch PTI on April 6th (3) Imran Alvi at 1PM on AAJ News. Rgds #PTI
Watch PTI on April 6th (1) Dr @ArifAlvi, Andleeb Abbas and Ehtisham Ameer at 11PM on ARY News (2) Hamid Khan at 10PM on ATV. Rgds #PTI
Watch PTI on April 5th (1) Farooq Amjad Meer at 10:45PM on Dunya News. Rgds #PTI
Watch PTI on April 4th (4) Faisal Khan at 8PM on PTV News. Rgds #PTI
@FaisalJavedKhan
Watch PTI on April 4th (3) Faisal Khan at 11PM on ATV. Rgds #PTI
@FaisalJavedKhan
Watch PTI on April 4th (1) Dr Israr Shah at 8PM on Waqt News (2) Dr Arif Alvi at 9PM on PTV World. Rgds #PTI
@ArifAlvi
Watch PTI on April 3rd (12) Abrar ul Haq on 10PM on Dawn News (13) Shabbir Sial at 10PM on Channel5. Rgds #PTI
Watch PTI on April 3rd (11) Sadaqat Abbasi on 8PM on RohiTV. Rgds #PTI
Watch PTI on April 3rd (10) Dr Zarqa and Andleeb Abbas on 8PM on Waqt News. Rgds #PTI
Watch PTI on April 3rd (9) Fauzia Kasuri at 8PM on Din News. Rgds #PTI

这是我从程序中得到的差异:

*** 
--- 
***************
*** 1,7 ****
- Watch PTI on April 7th (3) Malaika Reza at 8PM on AAJ News (4) Shah Mehmood Qureshi at 8PM on Geo News. Rgds #PTI
- Watch PTI on April 7th (2) Chairman IMRAN KHAN at 10PM on PTV News in News Night with Sadia Afzal, Rpt: 2AM, 2PM. Rgds #PTI
- @ImranKhanPTI
- Watch PTI on April 7th (1) Dr Waseem Shahzad NOW at 6PM on PTV News. Rgds #PTI
  Watch PTI on April 6th (7) Dr Israr Shah at 10PM on Business Plus in "Talking Policy". Rgds #PTI
  CORRECTION!! Watch PTI on April 6th (5) @Asad_Umar  at 8PM on ARY News. Rgds #PTI
  Watch PTI on April 6th (5) @Asad_Umar  at 8PM on AAJ News (6) PTI vs PMLN at 8PM on NewsOne. Rgds #PTI
--- 1,3 ----
***************
*** 21,24 ****
  Watch PTI on April 3rd (12) Abrar ul Haq on 10PM on Dawn News (13) Shabbir Sial at 10PM on Channel5. Rgds #PTI
  Watch PTI on April 3rd (11) Sadaqat Abbasi on 8PM on RohiTV. Rgds #PTI
  Watch PTI on April 3rd (10) Dr Zarqa and Andleeb Abbas on 8PM on Waqt News. Rgds #PTI
! Watch PTI on April 3rd (9) Fauzia Kasuri at 8PM on Din News. Rgds #PTI--- 17,23 ----
  Watch PTI on April 3rd (12) Abrar ul Haq on 10PM on Dawn News (13) Shabbir Sial at 10PM on Channel5. Rgds #PTI
  Watch PTI on April 3rd (11) Sadaqat Abbasi on 8PM on RohiTV. Rgds #PTI
  Watch PTI on April 3rd (10) Dr Zarqa and Andleeb Abbas on 8PM on Waqt News. Rgds #PTI
! Watch PTI on April 3rd (9) Fauzia Kasuri at 8PM on Din News. Rgds #PTI
! Watch PTI on April 3rd (8) Mehmood Rasheed at 8PM on ARY News. Rgds #PTI
! Watch PTI on April 3rd (7) Israr Abbasi (Repeat on Arp 4th) at 1:20AM and 1PM on Vibe TV. Rgds #PTI
! Watch PTI on April 3rd (5) Rao Fahad at 9PM on Express News (6) Dr Seems Zia at 10:30PM on Health TV. Rgds #PTI

通过快速比较两个源文件(PTITVProgs 和 new_tweets)可以看出,它们之间的区别在于 4 月 7 日的 3 条推文4 月 3 日的 3 条推文

我只希望 new_tweets 中不在 PTITVProgs 中的行出现在差异中。

但它会抛出一堆我不想看到的文本。我不知道差异输出中的 *** 1,7****** 1,3*** 代表什么...? 获得更改的行的正确方法是什么?

最佳答案

只需像这样解析 diff 的输出(如果需要,将 '-' 更改为 '+'):

#!/usr/bin/env python

# difflib_test

import difflib

file1 = open('/home/saad/Code/test/new_tweets', 'r')
file2 = open('/home/saad/PTITVProgs', 'r')

diff = difflib.ndiff(file1.readlines(), file2.readlines())
delta = ''.join(x[2:] for x in diff if x.startswith('- '))
print delta

关于python difflib比较文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15864641/

相关文章:

python - 不支持的媒体类型 Django API

javascript - 在 .txt 文件中插入要读取的新行

r - 如何在绘图 (ggplot2) 的文本注释中放置 +/- 加减运算符?

python - python的difflib.find_longest_match是怎么实现的?

python difflib print 仅匹配两个字符串之间的部分

python - 鼠标悬停时如何更改行颜色? Tkinter TreeView

python - 用 Python 编写 OAuth 提供程序 : guide + libraries?

python - 带分隔符的 Pandas groupby 连接

c++ - 简单的从文本文件中读取算法不起作用

python-2.7 - 将 diff 与漂亮的 soup 对象一起使用