python difflib comparing files

前端 未结 2 642
北恋
北恋 2020-12-05 07:44

I am trying to use difflib to produce diff for two text files containing tweets. Here is the code:

#!/usr/bin/env python

# difflib_test

import difflib

fil         


        
2条回答
  •  自闭症患者
    2020-12-05 08:39

    There are multiple diff styles and different functions exist for them in the difflib library. unified_diff, ndiff and context_diff.

    If you don't want the line number summaries, ndiff function gives a Differ-style delta:

    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)
    

    Output:

      1
    - 2          
      3          
      4          
      5   
    + 6
    

    EDIT:

    You could also parse the diff to extract only the changes if that's what you want:

    >>>changes = [l for l in diff if l.startswith('+ ') or l.startswith('- ')]
    
    >>>for c in changes:
           print(c)
    >>>
    - 2
    + 6
    

提交回复
热议问题