国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python difflib模塊示例講解

2020-01-04 16:44:33
字體:
來源:轉載
供稿:網友

difflib模塊提供的類和方法用來進行序列的差異化比較,它能夠比對文件并生成差異結果文本或者html格式的差異化比較頁面,如果需要比較目錄的不同,可以使用filecmp模塊。

class difflib.SequenceMatcher

此類提供了比較任意可哈希類型序列對方法。此方法將尋找沒有包含‘垃圾'元素的最大連續匹配序列。

通過對算法的復雜度比較,它由于原始的完形匹配算法,在最壞情況下有n的平方次運算,在最好情況下,具有線性的效率。

它具有自動垃圾啟發式,可以將重復超過片段1%或者重復200次的字符作為垃圾來處理。可以通過將autojunk設置為false關閉該功能。

class difflib.Differ

此類比較的是文本行的差異并且產生適合人類閱讀的差異結果或者增量結果,結果中各部分的表示如下:

python,difflib,模塊

class difflib.HtmlDiff

 此類可以被用來創建HTML表格 (或者說包含表格的html文件) ,兩邊對應展示或者行對行的展示比對差異結果。

 make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])

make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])

以上兩個方法都可以用來生成包含一個內容為比對結果的表格的html文件,并且部分內容會高亮顯示。

difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])

比較a與b(字符串列表),并且返回一個差異文本行的生成器
示例:

>>> 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])

返回最大匹配結果的列表

示例:

>>> 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])

比較a與b(字符串列表),返回一個Differ-style 的差異結果
示例:

>>> 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)

返回一個由兩個比對序列產生的結果

示例

>>> 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])

比較a與b(字符串列表),返回一個unified diff格式的差異結果.

示例:

>>> 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

實際應用示例

比對兩個文件,然后生成一個展示差異結果的HTML文件

#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()

運行結果:

python,difflib,模塊

生成的html文件比對結果:

python,difflib,模塊

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东台市| 涿州市| 仪陇县| 玉山县| 扶绥县| 高碑店市| 斗六市| 余姚市| 菏泽市| 益阳市| 邻水| 尤溪县| 托克逊县| 容城县| 长垣县| 怀来县| 吴旗县| 五原县| 盈江县| 金平| 凤城市| 钦州市| 巨鹿县| 伊通| 青河县| 鸡西市| 绩溪县| 阿拉善右旗| 海安县| 太仓市| 杨浦区| 天峻县| 怀化市| 昭觉县| 固原市| 葫芦岛市| 新乡市| 金门县| 葫芦岛市| 项城市| 石台县|