python difflib comparing files

前端 未结 2 648
北恋
北恋 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:19

    Just parse output of diff like this (change '- ' to '+ ' if needed):

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

提交回复
热议问题