python difflib模組講解示例
阿新 • • 發佈:2018-11-21
difflib模組提供的類和方法用來進行序列的差異化比較,它能夠比對檔案並生成差異結果文字或者html格式的差異化比較頁面,如果需要比較目錄的不同,可以使用filecmp模組。
class difflib.SequenceMatcher
此類提供了比較任意可雜湊型別序列對方法。此方法將尋找沒有包含‘垃圾’元素的最大連續匹配序列。通過對演算法的複雜度比較,它由於原始的完形匹配演算法,在最壞情況下有n的平方次運算,在最好情況下,具有線性的效率。它具有自動垃圾啟發式,可以將重複超過片段1%或者重複200次的字元作為垃圾來處理。可以通過將autojunk設定為false關閉該功能。
- 1
- 2
- 3
- 4
- 5
class difflib.Differ
此類比較的是文字行的差異並且產生適合人類閱讀的差異結果或者增量結果,結果中各部分的表示如下:
- 1
class difflib.HtmlDiff
此類可以被用來建立HTML表格 (或者說包含表格的html檔案) ,兩邊對應展示或者行對行的展示比對差異結果。 make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
- 1
- 2
- 3
- 4
- 5
以上兩個方法都可以用來生成包含一個內容為比對結果的表格的html檔案,並且部分內容會高亮顯示。
difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
比較a與b(字串列表),並且返回一個差異文字行的生成器
- 1
示例:
>>> 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
difflib.get_close_matches(word, possibilities[, n][, cutoff])
返回最大匹配結果的列表
- 1
示例:
>>> 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']
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
difflib.ndiff(a, b[, linejunk][, charjunk])
比較a與b(字串列表),返回一個Differ-style 的差異結果
- 1
示例:
>>> diff = ndiff('one\ntwo\nthree\n'.splitlines(1),... 'ore\ntree\nemu\n'.splitlines(1))>>> print ''.join(diff),- one? ^+ ore? ^- two- three? -+ tree+ emu
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
difflib.restore(sequence, which)
返回一個由兩個比對序列產生的結果
- 1
示例
>>> 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
difflib.unified_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
比較a與b(字串列表),返回一個unified diff格式的差異結果.
- 1
示例:
>>> 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+++ [email protected]@ -1,4 +1,4 @@-bacon-eggs-ham+python+eggy+hamster guido
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
實際應用示例
比對兩個檔案,然後生成一個展示差異結果的HTML檔案
#coding:utf-8'''file:difflibeg.pydate:2017/9/9 10:33author:lockeyemail:[email protected]: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()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
執行結果:
生成的html檔案比對結果:
再分享一下我老師大神的人工智慧教程吧。零基礎!通俗易懂!風趣幽默!希望你也加入到我們人工智慧的隊伍中來!http://www.captainbed.net