2013-04-07 25 views
10

Sto cercando di usare difflib per produrre diff per due file di testo contenenti tweet. Ecco il codice:python difflib confronto file

#!/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 

Ecco il file PTITVProgs di testo:

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 

Ecco il file new_tweets di testo:

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 

Ecco il diff ottengo dal programma:

*** 
--- 
*************** 
*** 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 

Come puoi vedere confrontando rapidamente i due file di origine (PTITVProgs e new_tweets) che la differenza tra loro è i 3 tweet che erano il 7 aprile e 3 tweet il 3 aprile.

Voglio solo le linee in new_tweets che non sono in PTITVProgs per apparire nel diff.

Ma getta un po 'di testo che non voglio vedere. Non so cosa *** 1,7*** e *** 1,3*** nel supporto di uscita diff per ...? Qual è il modo corretto per ottenere solo le linee modificate??

+1

non sono sicuro difflib è lo strumento giusto per il lavoro che si sta cercando di fare a tutti - è molto più lavoro (algoritmicamente parlando) per generare una differenza rispetto a fare solo un confronto di set. 'print set (fileA.readlines()). difference (set (fileB.readlines()))' –

risposta

20

Basta analizzare l'output di diff come questo (cambiamento '-' a '+' se necessario):

#!/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 
14

Nella libreria difflib esistono diversi stili di diff e diverse funzioni. unified_diff, ndiff e context_diff.

Se non si desidera che i riepiloghi numero di riga, ndiff funzione fornisce un delta stile Differ-:

import difflib 

f1 = '''1 
2 
3 
4 
5''' 
f2 = '''1 
3 
4 
5 
6''' 

diff = difflib.ndiff(f1,f2) 

for l in diff: 
    print(l) 

uscita:

1 
- 2   
    3   
    4   
    5 
+ 6 

EDIT:

Si potrebbe analizza anche il diff per estrarre solo le modifiche se questo è ciò che desideri:

>>>changes = [l for l in diff if l.startswith('+ ') or l.startswith('- ')] 

>>>for c in changes: 
     print(c) 
>>> 
- 2 
+ 6 
+1

Da come ho letto la domanda l'OP non vuole neanche le linee di contesto. La risposta di @ gatto sopra fa un lavoro migliore che corrisponde al formato che l'OP vuole. –

Problemi correlati