神剑山庄资源网 Design By www.hcban.com

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
"color: #800000">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)),
one
two
three
> print ''.join(restore(diff, 2)),
ore
tree
emu

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

运行结果:

python difflib模块示例讲解

生成的html文件比对结果:

python difflib模块示例讲解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python,difflib,模块

神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com

评论“python difflib模块示例讲解”

暂无python difflib模块示例讲解的评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。