python difflib模塊示例講解
difflib模塊提供的類和方法用來進(jìn)行序列的差異化比較,它能夠比對(duì)文件并生成差異結(jié)果文本或者h(yuǎn)tml格式的差異化比較頁面,如果需要比較目錄的不同,可以使用filecmp模塊。
class difflib.SequenceMatcher
此類提供了比較任意可哈希類型序列對(duì)方法。此方法將尋找沒有包含‘垃圾'元素的最大連續(xù)匹配序列。
通過對(duì)算法的復(fù)雜度比較,它由于原始的完形匹配算法,在最壞情況下有n的平方次運(yùn)算,在最好情況下,具有線性的效率。
它具有自動(dòng)垃圾啟發(fā)式,可以將重復(fù)超過片段1%或者重復(fù)200次的字符作為垃圾來處理??梢酝ㄟ^將autojunk設(shè)置為false關(guān)閉該功能。
class difflib.Differ
此類比較的是文本行的差異并且產(chǎn)生適合人類閱讀的差異結(jié)果或者增量結(jié)果,結(jié)果中各部分的表示如下:

class difflib.HtmlDiff
此類可以被用來創(chuàng)建HTML表格 (或者說包含表格的html文件) ,兩邊對(duì)應(yīng)展示或者行對(duì)行的展示比對(duì)差異結(jié)果。
make_file(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
make_table(fromlines, tolines [, fromdesc][, todesc][, context][, numlines])
以上兩個(gè)方法都可以用來生成包含一個(gè)內(nèi)容為比對(duì)結(jié)果的表格的html文件,并且部分內(nèi)容會(huì)高亮顯示。
difflib.context_diff(a, b[, fromfile][, tofile][, fromfiledate][, tofiledate][, n][, lineterm])
比較a與b(字符串列表),并且返回一個(gè)差異文本行的生成器
示例:
>>> 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])
返回最大匹配結(jié)果的列表
示例:
>>> 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(字符串列表),返回一個(gè)Differ-style 的差異結(jié)果
示例:
>>> 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)
返回一個(gè)由兩個(gè)比對(duì)序列產(chǎn)生的結(jié)果
示例
>>> 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])
比較a與b(字符串列表),返回一個(gè)unified diff格式的差異結(jié)果.
示例:
>>> 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
實(shí)際應(yīng)用示例
比對(duì)兩個(gè)文件,然后生成一個(gè)展示差異結(jié)果的HTML文件
#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','a+') as fo:
fo.write(hd.make_file(loads,mems))
fo.close()
運(yùn)行結(jié)果:

生成的html文件比對(duì)結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Python對(duì)網(wǎng)易云歌單數(shù)據(jù)分析及可視化
這篇文章主要介紹了使用Python對(duì)網(wǎng)易云歌單數(shù)據(jù)分析及可視化,本項(xiàng)目以數(shù)據(jù)采集、處理、分析及數(shù)據(jù)可視化為項(xiàng)目流程,需要的朋友可以參考下2023-03-03
Django JWT Token RestfulAPI用戶認(rèn)證詳解
這篇文章主要介紹了Django JWT Token RestfulAPI用戶認(rèn)證詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
python3新特性函數(shù)注釋Function Annotations用法分析
這篇文章主要介紹了python3新特性函數(shù)注釋Function Annotations用法,結(jié)合實(shí)例形式分析了Python3函數(shù)注釋的定義方法與使用技巧,需要的朋友可以參考下2016-07-07
PythonWeb項(xiàng)目Django部署在Ubuntu18.04騰訊云主機(jī)上
這篇文章主要介紹了PythonWeb項(xiàng)目Django部署在Ubuntu18.04騰訊云主機(jī)上的相關(guān)知識(shí),本文通過代碼加文字說明的形式給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04
Python實(shí)現(xiàn)語音識(shí)別Whisper的使用示例
Whisper是由OpenAI基于Python開發(fā)的能夠識(shí)別多國(guó)語言的語音識(shí)別模型,本文主要介紹了Python實(shí)現(xiàn)語音識(shí)別Whisper的使用示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
python 讀入多行數(shù)據(jù)的實(shí)例
下面小編就為大家分享一篇python 讀入多行數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
對(duì)tensorflow中tf.nn.conv1d和layers.conv1d的區(qū)別詳解
今天小編就為大家分享一篇對(duì)tensorflow中tf.nn.conv1d和layers.conv1d的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
python連接mysql數(shù)據(jù)庫并讀取數(shù)據(jù)的實(shí)現(xiàn)
這篇文章主要介紹了python連接mysql數(shù)據(jù)庫并讀取數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

