Compare file contents using the Difflib module
1 Example: string difference comparison
Vim duibi.py
#!/usr/bin/env python# -*- coding: utf-8 -*-import reimport osimport Difflibtex1= "" "Tex1:this is a test for difflib ,just try to get difference of the log now try to determine whether the function is feasible good goodtest so try it. Good "" "Tex1_lines=tex1.splitlines () tex2 = "" "tex2:this is a test for difflib ,just try to get Difference of the log now try the function is feasible goodtast so try it "" "Tex2_lines=tex2.splitlines () #--------- The original contrast method----------#d =difflib. Differ () #diff =d.compare (tex1_lines,tex2_lines) #print ' \ n '. Join (List diff) #--------HTML comparison Method----------# and modify the diff.html encoding, change iso-8859-1 to UTF-8 format parsing file, used to compare Chinese d=difflib. Htmldiff () q=d.make_file (tex1_lines,tex2_lines) old_str= ' charset=iso-8859-1 ' new_str= ' charset=UTF-8 ' with Open (' diff.html ', ' W ') as f_new:f_new.write (Q.replace (OLD_STR,NEW_STR)) ############################## D=difflib. Htmldiff () #q =d.make_file (tex1_lines,tex2_lines) #old_Str= ' charset=iso-8859-1 ' #new_str = ' charset=utf-8 ' #data =q.replace (old_str,new_str) #fo =open (' diff.html ', ' W ') # Fo.write (data) #fo. Close () ############################
Run Python duibi.py production diff.html
The browser opens diff.html to view the comparison results.
2 sample file Comparison file diff code can be used directly without modification (including Chinese)
Compare the differences of Testfile1 Testfile2 with the following script
Vim diff.py
#!/usr/bin/env python# -*- coding: utf-8 -*-import osimport sysimport difflibtry:tfile1=sys.argv[1]tfile2=sys.argv[2]except exception,e:print "Error:" +str (e) print " Please enter the exact parameters, for example: Python diff.py file1 file2 "Sys.exit () def readfile (filename): Try:filehandle=open (filename, ' RB ') Lines=filehandle.read (). Splitlines () filehandle.close () Return linesexcept ioerror as error:print (' Read file error: ' +str (Error) ') Sys.exit () if tfile1== "" or tfile2== "":p rint "Please input parameters accurately, e.g. python diff.py file1 file2" Sys.exit () tfile1_lines=readfile (tfile1) tfile2_ Lines=readfile (tfile2) #d =difflib. Htmldiff () #print s.make_file (tfile1_lines,tfile2_lines) #为了生成html能识别中文, use the following code # Modify the diff.html encoding to change the iso-8859-1 to utf-8#==================================== #方法1: #d =difflib. Htmldiff () #q =d.make_file (tfile1_lines,tfile2_lines) #old_str = ' charset=iso-8859-1 ' #new_str = ' charset=utf-8 ' #data = Q.replace (OLD_STR,NEW_STR) #fo =open (' diff.HTML ', ' W ') #fo. Write (data) #fo. Close () #==================================== #方法2: #d =difflib. Htmldiff () #q =d.make_file (tfile1_lines,tfile2_lines) #old_str = ' charset=iso-8859-1 ' #new_str = ' charset=utf-8 ' #fo = Open (' diff.html ', ' W ') #fo. Write (q) #fo. Close () #with open (' diff.html ', ' R ') as f:# lines=f.readlines () #with open (' diff.html ', ' W ') as f_new:# for line in lines:# f_new.write (Line.replace (old_str,new_ STR)) #===================================== #方法3: old_str= ' charset=iso-8859-1 ' new_str= ' charset=utf-8 ' d=difflib. Htmldiff () q=d.make_file (tfile1_lines,tfile2_lines) with open (' diff.html ', ' W ') as f_new:f_ New.write (Q.replace (OLD_STR,NEW_STR))
Execute python diff.py testfile1 testfile2
Generate diff.html
Browser View File comparison results
Python using difflib Comparison file example