python difflib模块示例讲解


Posted in Python onSeptember 13, 2017

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)),
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 相关文章推荐
pymssql数据库操作MSSQL2005实例分析
May 25 Python
Python os模块学习笔记
Jun 21 Python
python 读写txt文件 json文件的实现方法
Oct 22 Python
Python实现的堆排序算法原理与用法实例分析
Nov 22 Python
Python中pygal绘制雷达图代码分享
Dec 07 Python
在python中利用最小二乘拟合二次抛物线函数的方法
Dec 29 Python
对pandas处理json数据的方法详解
Feb 08 Python
Python正则表达式学习小例子
Mar 03 Python
浅谈django 重载str 方法
May 19 Python
Selenium Webdriver元素定位的八种常用方式(小结)
Jan 13 Python
Pytorch distributed 多卡并行载入模型操作
Jun 05 Python
opencv-python图像配准(匹配和叠加)的实现
Jun 23 Python
Python网络编程 Python套接字编程
Sep 13 #Python
python和ruby,我选谁?
Sep 13 #Python
python实现简单点对点(p2p)聊天
Sep 13 #Python
django 常用orm操作详解
Sep 13 #Python
Python三级菜单的实例
Sep 13 #Python
基于Python代码编辑器的选用(详解)
Sep 13 #Python
python多线程socket编程之多客户端接入
Sep 12 #Python
You might like
Zend引擎的发展 [15]
2006/10/09 PHP
Yii 快速,安全,专业的PHP框架
2014/09/03 PHP
php遍历目录方法小结
2015/03/10 PHP
Javascript 生成指定范围数值随机数
2009/01/09 Javascript
js相册效果代码(点击创建即可)
2013/04/16 Javascript
Javascript和HTML5利用canvas构建Web五子棋游戏实现算法
2013/07/17 Javascript
js判断两个日期是否相等的方法
2013/09/10 Javascript
JS图像无缝滚动脚本非常好用
2014/02/10 Javascript
jquery mobile页面跳转后样式丢失js失效的解决方法
2014/09/06 Javascript
JS实现同一个网页布局滑动门和TAB选项卡实例
2015/09/23 Javascript
JavaScript动态生成二维码图片
2016/04/20 Javascript
利用纯Vue.js构建Bootstrap组件
2016/11/03 Javascript
JavaScript常用正则函数用法示例
2017/01/23 Javascript
vue cli2.0单页面title修改方法
2018/06/07 Javascript
详解swipe使用及竖屏页面滚动方法
2018/06/28 Javascript
微信小程序引用iconfont图标的方法
2018/10/22 Javascript
[01:59]游戏“zheng”当时试玩会
2019/08/21 DOTA
python中函数默认值使用注意点详解
2016/06/01 Python
Python对象类型及其运算方法(详解)
2017/07/05 Python
python 将数据保存为excel的xls格式(实例讲解)
2018/05/03 Python
基于Pandas读取csv文件Error的总结
2018/06/15 Python
10个Python小技巧你值得拥有
2018/09/29 Python
python  ceiling divide 除法向上取整(或小数向上取整)的实例
2019/12/27 Python
pycharm实现在子类中添加一个父类没有的属性
2020/03/12 Python
python 实现分组求和与分组累加求和代码
2020/05/18 Python
使用python批量修改XML文件中图像的depth值
2020/07/22 Python
浅析python函数式编程
2020/09/26 Python
美国家具网站:Cymax
2016/09/17 全球购物
加拿大大码女装购物网站:Penningtons
2020/12/26 全球购物
毕业生自荐书
2014/02/03 职场文书
少先队入队活动方案
2014/02/08 职场文书
《美丽的公鸡》教学反思
2014/02/25 职场文书
志愿者宣传口号
2014/06/17 职场文书
优秀党员先进材料
2014/12/18 职场文书
正规借条模板
2015/05/26 职场文书
婚礼双方父亲致辞
2015/07/27 职场文书