Difflib module file content difference comparison

Introduction    

      As a standard library module of python, difflib does not need to be installed. Its function is to compare the differences between texts, and it supports the output of HTML documents with strong readability, which is similar to the diff command under Linux. You can use this module to compare the differences between code and configuration files, which is very useful in version control. Python 2.3 and later versions come with the difflib module by default, and no additional installation is required.

Instructions

  • String difference comparison
import difflib
text1 = """text1:"""
text1 = """text1: This module provide classes and functions for comparing sequences"""
text1_lines = text1.splitlines()
text2 = """text2: This module provide classes and functions for comparing sequences v1.2"""
text2_lines = text2.splitlines()
d = difflibe.Differ()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
NameError: name 'difflibe' is not defined
d = difflib.Differ()
diff = d.compare(text1_lines,text2_lines)
print '\n'.join(list(diff))

- text1: This module provide classes and functions for comparing sequences
?     ^
+ text2: This module provide classes and functions for comparing sequences v1.2
?     ^                                                                   +++++ 

     The meaning of each difference symbol

'-': included in the first sequence line, not included in the second sequence line

'+': included in the second sequence line, not included in the first sequence line

'': The two sequence lines are consistent

'?': Marks incremental differences between two sequence lines

'^': mark the difference between two sequences

  • Generate beautiful HTML documents
  • Compare nginx configuration file differences

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324936350&siteId=291194637