Python difflib module example, pythondifflib
The classes and methods provided by the difflib module are used for differential comparison of sequences. They can compare files and generate differentiated comparison pages for different result texts or html formats. If you need to compare different directories, you can use the filecmp module.
Class difflib. SequenceMatcher
This class provides methods for comparing any hash type sequence pairs. This method searches for the maximum continuous matching sequence that does not contain the 'split' element.
By comparing the complexity of the algorithm, it has n square operations in the worst case due to the original shape matching algorithm, and has linear efficiency in the best case.
It has an automatic spam heuristic and can process characters that have already exceeded 1% fragments or repeated 200 times as spam. You can disable this function by setting autojunk to false.
Class difflib. Differ
This type of comparison compares the differences between text lines and produces different results or incremental results that are suitable for human reading. The results are represented in the following parts:
Class difflib. HtmlDiff
This class can be used to create HTML tables (or html files containing tables). The two sides correspond to the presentation or comparison results of rows.
Make_file (fromlines, tolines [, fromdesc] [, todesc] [, context] [, numlines])
Make_table (fromlines, tolines [, fromdesc] [, todesc] [, context] [, numlines])
The preceding two methods can be used to generate an html file containing a table whose content is the comparison result, and some of the content will be highlighted.
Difflib. context_diff (a, B [, fromfile] [, tofile] [, fromfiledate] [, tofiledate] [, n] [, lineterm])
Compares a and B (string list) and returns a generator of a different text line.
Example:
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\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 the 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])
Compares a and B (string list) and returns the result of a Differ-style difference.
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 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>>> print ''.join(restore(diff, 1)),onetwothree>>> print ''.join(restore(diff, 2)),oretreeemu
Difflib. unified_diff (a, B [, fromfile] [, tofile] [, fromfiledate] [, tofiledate] [, n] [, lineterm])
Compare a and B (string list), and return a result of difference in the uniied diff format.
Example:
>>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']>>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\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 @@-bacon-eggs-ham+python+eggy+hamster guido
Example
Compare the two files and generate an HTML file that displays the different 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()
Running result:
Comparison result of the generated html file:
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.