Built in #### python difflib text comparison module ### of

difflib for the python standard library module, without having to install
the role of contrast is the difference between the text and strong support output more readable HTML document, similar to the diff command under Linux.

difflib module classes and methods used to perform the sequence comparison of the so-called difference, it is possible to compare differences in the results file and said file or html format difference comparison page
text document 1. Comparative differences in the results generated

Difference resulting text symbols appreciated

symbol meaning
‘-’ Contained in the first row in the series, but not the second
‘+’ Contained in the second row in the series, but does not include the first
’ ’ Two series of the same row
‘?’ Incremental differences exist
‘^’ Differences in character

First text line segmentation by
text1 = splitlines (keepends = False) # multiple lines of text segmentation by line, to return a list of end of line is not retained newline
text2 = splitlines (keepends = True) # segmented by multiple lines of text lines, returns a list of reserved line feed end of the line
to generate differences in the results file
import difflib # import modules
diff = difflib.Differ () # difference generates an object
result = diff.compare (text1, text2) # difference using the difference comparing two text list objects, differences in the results generated objects
generated by the object can not directly see the difference, need to be converted into a list and generate the link text

mport  difflib
text1 = '''  1. Beautiful is better than ugly.
       2. Explicit is better than implicit.
       3. Simple is better than complex.
       4. Complex is better than complicated.
		'''.splitlines(keepends=True)
print(text1)

text2 = '''  1. Beautiful is better than ugly.
       3.   Simple is better than complex.
       4. Complicated is better than complex.
       5. Flat is better than nested.
     '''.splitlines(keepends=True)
print(text2)

diff= difflib.Differ()
result =''.join(list(diff.compare(text1,text2)))
print(result)
运行结果:
['  1. Beautiful is better than ugly.\n', '       2. Explicit is better than implicit.\n', '       3. Simple is better than complex.\n', '       4. Complex is better than complicated.\n', '\t\t']   #text1 按行分割后生成的列表
['  1. Beautiful is better than ugly.\n', '       3.   Simple is better than complex.\n', '       4. Complicated is better than complex.\n', '       5. Flat is better than nested.\n', '     ']    #text2 按行分割后生成的列表
差异文本
    1. Beautiful is better than ugly.
-        2. Explicit is better than implicit.
-        3. Simple is better than complex.
+        3.   Simple is better than complex.
?          ++
-        4. Complex is better than complicated.
?                 ^                     ---- ^
+        4. Complicated is better than complex.
?                ++++ ^                      ^
- 		+        5. Flat is better than nested.
+      

Comparative html format file generated difference comparison page
1. The text is divided by line
2. The differences in the results file generated
diff = difflib.HtmlDiff () # difference generates an object
result = diff.make_file (text1, text2) # comparative differences in the results generated
3. the difference in results is written in html file
with Open ( 'diff.html', 'W') AS F:
f.write (result)

import  difflib
text1 = '''  1. Beautiful is better than ugly.
       2. Explicit is better than implicit.
       3. Simple is better than complex.
       4. Complex is better than complicated.
		'''


text2 = '''  1. Beautiful is better than ugly.
       3.   Simple is better than complex.
       4. Complicated is better than complex.
       5. Flat is better than nested.
     '''.


text1=splitlines(keepends=True)
text2=splitlines(keepends=True)
diff = difflib.HtmlDiff()
result = diff.make_file(text1,text2)

with open('diff.html','w') as f:
    f.write(result)

Use browser to open the html file generated by the different places will be displayed in a different color
Here Insert Picture Description

Here Insert Picture Description
Comparison between files
must first read the contents of the file, then the text comparison method described above can be a

import difflib

filename1 = '/tmp/passwd'
filename2 = '/tmp/passwd1'
with open(filename1) as f1,open(filename2) as f2:   #打开文件
    content1 = f1.read().splitlines(keepends=True)   #打开文件1读取内容并按行分割
    content2 = f2.read().splitlines(keepends=True)
d = difflib.HtmlDiff()
htmlContent = d.make_file(content1,content2)
with open('passwdDiff.html','w') as f:
    f.write(htmlContent)

Guess you like

Origin blog.csdn.net/weixin_44821839/article/details/91825261