This article mainly for you in detail the Python difflib module example, with a certain reference value, interested in small partners can refer to
The classes and methods provided by the Difflib module are used for the differential comparison of sequences, which can be compared to a file and generate differential results text or HTML-formatted diff pages, and if you need to compare directories, you can use the FILECMP module.
Class Difflib. Sequencematcher
This class provides a way to compare any hash type sequence pair. This method will look for the maximum contiguous sequence of matches that do not contain the ' garbage ' element.
By comparing the complexity of the algorithm, it is due to the original completion matching algorithm, in the worst case there is N squared operation, in the best case, with linear efficiency.
It has an automatic garbage heuristic that allows you to treat characters that repeat more than fragment 1% or repeat 200 times as garbage. You can turn off this feature by setting Autojunk to False.
Class Difflib. Differ
This class compares the differences between lines of text and produces a difference or incremental result that is appropriate for human reading, and the parts of the results are expressed as follows:
Class Difflib. Htmldiff
This class can be used to create an HTML table (or an HTML file that contains a table), with either a display or row-to-row comparison of the results.
Make_file (Fromlines, Tolines [, fromdesc][, todesc][, context][, Numlines])
Make_table (Fromlines, Tolines [, fromdesc][, todesc][, context][, Numlines])
The above two methods can be used to generate an HTML file containing a table with a comparison of the results, and some of the content will be highlighted.
Difflib.context_diff (A, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, Lineterm])
Compares A with B (String list) and returns a generator with a differential text line
Example:
>>> S1 = [' bacon\n ', ' eggs\n ', ' ham\n ', ' guido\n ']>>> s2 = [' python\n ', ' eggy\n ', ' hamster\n ', ' guido\n ' ]>>> in Context_diff (S1, S2, fromfile= ' before.py ', tofile= ' after.py '): ... Sys.stdout.write (line) * * * before.py---after.py****************** 1,4 ****! bacon! eggs! Ham Guido---1,4----! Python! eggy! Hamster Guido
Difflib.get_close_matches (Word, possibilities[, n][, cutoff])
Returns a list of the maximum matching results
Example:
>>> get_close_matches (' appel ', [' Ape ', ' apple ', ' peach ', ' puppy ']) [' Apple ', ' ape ']>>> import keyword >>> get_close_matches (' wheel ', keyword.kwlist) [' While ']>>> get_close_matches (' Apple ', keyword.kwlist) []>>> get_close_matches (' Accept ', keyword.kwlist) [' except ']
Difflib.ndiff (A, b[, linejunk][, Charjunk])
Compare A with B (String list), return a differ-style difference result
Example:
>>> diff = Ndiff (' one\ntwo\nthree\n '. Splitlines (1),... ' Ore\ntree\nemu\n '. Splitlines (1)) >>> print '. Join (diff),-one? ^+ ore? ^-Two-three? -+ tree+ EMU
Difflib.restore (sequence, which)
Returns a result of two comparison sequences
Example
>>> diff = Ndiff (' one\ntwo\nthree\n '. Splitlines (1),... ' Ore\ntree\nemu\n '. Splitlines (1)) >>> diff = list (diff) # materialize the generated delta into a list>>> p Rint ". Join (Restore (diff, 1)),onetwothree>>> print". Join (Restore (diff, 2)), Oretreeemu
Difflib.unified_diff (A, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, Lineterm])
Compare A with B (String list) and return a unified diff format difference result.
Example:
>>> S1 = [' bacon\n ', ' eggs\n ', ' ham\n ', ' guido\n ']>>> s2 = [' python\n ', ' eggy\n ', ' hamster\n ', ' guido\n ' ]>>> in Unified_diff (S1, S2, fromfile= ' before.py ', tofile= ' after.py '): ... Sys.stdout.write (line)---before.py+++ after.py@@ -1,4 +1,4 @@-bacon-eggs-ham+python+eggy+hamster Guido
Practical Application Examples
Compare two files and then generate an HTML file that shows the difference results
#coding: Utf-8 ' FILE:DIFFLIBEG.PYDATE:2017/9/9 10:33author:lockeyemail:lockey@123.comdesc:diffle module Learning and practising ' import DIFFLIBHD = Difflib. Htmldiff () loads = ' with open (' g:/python/note/day09/0907code/hostinfo/cpu.py ', ' R ') as Load:loads = Load.readlines () Load.close () MEMS = ' with open (' g:/python/note/day09/0907code/hostinfo/mem.py ', ' R ') as Mem:mems = Mem.readlines () Mem.close () with open (' htmlout.html ', ' A + ') as Fo:fo.write (Hd.make_file (Loads,mems)) Fo.close ()
Operation Result:
The resulting HTML file is compared to the result: