2.1 document content difference comparison method
This section describes how to realize the difference comparison of file contents through difflib module. As a standard library module of Python, difflib does not need to be installed. It is used to compare the differences between texts and supports the output of HTML documents with strong readability. Similar to diff commands under Linux, we can use difflib to compare the differences between codes and configuration files, which is very useful in version control
2.1.1 example 1: comparison of differences between two strings
This example realizes the difference comparison between two strings by using difflib module, and then outputs them in version control style
[root@mankel py] vim test-1122.py import difflib text1 = """ #Definition string 1 Difflib is a module used to compare file content differences in Python It can help us find mistakes """ text1_lines = text1.splitlines() #Separated by rows for comparison text2 = """ #Definition string 2 Diffli is a module used to compare file content differences in pytho It can Help us find mistakes """ text2_lines = text2.splitlines() d = difflib.Differ() #Create a different() object diff = d.compare(text1_lines,text2_lines) #Compare strings using the compare method print('\n'.join(list(diff)))
1) This example uses the diff() class to compare two strings. In addition, the SequenceMatcher() class of difflib supports the comparison of sequences of any type, and the HtmlDiff() class supports the output of the comparison results in HTML format. The example operation results are as follows:
[root@mankel py] python3 test-1122.py - Difflib is a module used to compare file content differences in Python ? - ^ - + Diffli is a module used to compare file content differences in pytho ? ^ - It can help us find mistakes ? ^ + It can Help us find mistakes ? ^
2) 2. Description of symbol meaning
'-': included in the first sequence line, but not in the second sequence line
'+': included in the second sequence line, but not in the first sequence line
'': the two sequence lines are consistent
‘?’ : Indicates that there is an incremental difference between the two sequence lines
'^': a character that indicates the difference between two sequence lines
2.1.2 generate beautiful contrast HTML format documents
Make using HtmlDiff() class_ The file () method can generate a beautiful HTML document. Click to modify the code in example 1:
d = difflib.Differ() diff = d.compare(text1_lines,text2_lines) print('\n'.join(list(diff)))
replace with
d = difflib.HtmlDiff() diff = d.make_file(text1_lines,text2_lines) print('\n'.join(list(diff)))
Change the file name to test2-1122.py and run
[root@mankel py]# python3 test2-1122.py > test.html
Then open it through the browser. Through the highlighted part, we can quickly see the problem
2.1.3 example 2: compare differences in Nginx configuration files
When maintaining multiple nginx configurations, we often compare the differences of configuration files of different versions, so that the operation and maintenance personnel can more clearly understand the update items after iteration of different versions. The implementation idea is to read the two configuration files to be compared, and then use the newline character as the separator to call difflib.HtmlDiff() to generate the difference document in HTML format. The implementation code is as follows:
[root@mankel py] vim test3-1122.py #!/usr/bin/python3 import difflib import sys try: textfile1 = sys.argv[1] #First profile path parameter textfile2 = sys.argv[2] #Second profile path parameter except Exception as e: print("Error:"+str(e)) print("Usage: test3-1122.py filename1 filename2") sys.exit() def readfile(filename): #File read separator function try: fileHandle = open(filename,'r') text = fileHandle.read().splitlines() #Separated by rows after reading fileHandle.close() return text except IOError as error: print("Read file Error:"+str(error)) sys.exit() if textfile1=="" or textfile2=="": print("Usage: test3-1122.py filename1 filename2") sys.exit() text1_lines = readfile(textfile1) #Call the redfile function to get the separated string text2_lines = readfile(textfile2) d = difflib.HtmlDiff() #Create HtmlDiff() class object print(d.make_file(text1_lines,text2_lines)) #By making_ File method output
1) Comparison results in. HTML format
The running code is as follows: generate the file test2.html and continue to open it with the browser:
[root@mankel py]# python3 test3-1122.py nginx.conf.v1 nginx.conf.v2 > test2.html
2).success:
By highlighting, the difference is clear at a glance