The classes and methods provided by the Difflib module are used to make a differential comparison of the sequences, which can be compared to the file and generate the difference results text or HTML format of the diff page, and if you need to compare the catalog, use the FILECMP module.
class Difflib. Sequencematcher
This class provides a way to compare the sequence pairs of any hash type. This method will look for the largest contiguous sequence of matches that do not contain the ' garbage ' element.
by comparing the complexity of the algorithm, because of the original completion matching algorithm, in the worst case, there are n squared operations, in the best case, with linear efficiency.
it has an automatic garbage heuristic that can be treated as garbage by repeating characters that are more than 1% or repeated 200 times. You can turn off this feature by setting Autojunk to False.
class Difflib. Differ
This class compares the differences in lines of text and produces differences or incremental results that are appropriate for human reading, and the parts of the results are represented as follows:
class Difflib. Htmldiff
This class can be used to create an HTML table (or an HTML file that contains a table), with both sides corresponding to the display or row to show the difference results.
make_file (Fromlines, Tolines [, fromdesc][, todesc][, context][, Numlines])
make_table (Fromlines, Tolines [, fromdesc][, todesc][, context][, Numlines])
All two of these 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])
Compare A with B (list of strings) and return a generator for a different line of text
Example:
>>> S1 = [' bacon\n ', ' eggs\n ', ' ham\n ', ' guido\n ']
>>> s2 = [' python\n ', ' eggy\n ', ' hamster\n ', ' GUI Do\n ']
>>> for line 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 largest 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 (list of strings) and 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 the result of a sequence of two pairs
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
;>> print '. Join (Restore (diff, 1)), one
two
three
>>> print '. Join (Restore (diff, 2) ),
ore
tree
EMU
Difflib.unified_diff (A, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, Lineterm])
Compare A with B (list of strings) to return a difference result in a unified diff format.
Example:
>>> S1 = [' bacon\n ', ' eggs\n ', ' ham\n ', ' guido\n ']
>>> s2 = [' python\n ', ' eggy\n ', ' hamster\n ', ' gu ' Ido\n ']
>>> for line in Unified_diff (S1, S2, fromfile= ' before.py ', tofile= ' after.py '):
... Sys.stdout.write (line)
---before.py
+++
after.py @@ -1,4 +1,4 @@ -1,4-eggs
-ham
+python
+eggy
+hamster
Guido
Actual application Examples
Compare to two files, and then generate an HTML file showing the difference results
#coding: Utf-8
'
file:difflibeg.py
date:2017/9/9 10:33
author:lockey
email:lockey@123.com
desc:diffle Module Learning and practising
'
import difflib
HD = 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 ', ' + ') as fo:
fo.write ( Hd.make_file (Loads,mems))
Fo.close ()
Run Result:
Generated HTML file alignment result: