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 相关文章推荐
Python greenlet实现原理和使用示例
Sep 24 Python
Python 爬虫的工具列表大全
Jan 31 Python
利用Python写一个爬妹子的爬虫
Jun 08 Python
python中从str中提取元素到list以及将list转换为str的方法
Jun 26 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
Aug 02 Python
在python中实现强制关闭线程的示例
Jan 22 Python
python3.6下Numpy库下载与安装图文教程
Apr 02 Python
Python基于scipy实现信号滤波功能
May 08 Python
django xadmin中form_layout添加字段显示方式
Mar 30 Python
Python实现进度条和时间预估的示例代码
Jun 02 Python
Keras load_model 导入错误的解决方式
Jun 09 Python
Pandas实现DataFrame的简单运算、统计与排序
Mar 31 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
Windows PHP5和Apache的安装与配置
2009/06/08 PHP
[原创]ThinkPHP中SHOW_RUN_TIME不能正常显示运行时间的解决方法
2015/10/10 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
2018/02/06 PHP
Laravel框架路由设置与使用示例
2018/06/12 PHP
PHP实现简单注册登录系统
2020/12/28 PHP
jQuery 使用手册(二)
2009/09/23 Javascript
JS连接SQL数据库与ACCESS数据库的方法实例
2013/11/21 Javascript
javascript中的取反再取反~~没有意义
2014/04/06 Javascript
Bootstrap每天必学之基础排版
2015/11/20 Javascript
深入理解JavaScript中的call、apply、bind方法的区别
2016/05/30 Javascript
canvas绘制七巧板
2017/02/03 Javascript
详解vue.js全局组件和局部组件
2017/04/10 Javascript
JavaScript之创意时钟项目(实例讲解)
2017/10/23 Javascript
解决js ajax同步请求造成浏览器假死的问题
2018/01/18 Javascript
浅析Node.js非对称加密方法
2018/01/29 Javascript
vue+webpack实现异步加载三种用法示例详解
2018/04/24 Javascript
微信小程序自定义带价格显示日历效果
2018/12/29 Javascript
[02:17]DOTA2亚洲邀请赛 RAVE战队出场宣传片
2015/02/07 DOTA
[01:10:02]IG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.19
2018/08/21 DOTA
在Python中用split()方法分割字符串的使用介绍
2015/05/20 Python
Python开发的实用计算器完整实例
2017/05/10 Python
Python 在字符串中加入变量的实例讲解
2018/05/02 Python
python使用 __init__初始化操作简单示例
2019/09/26 Python
python实现简单井字棋游戏
2020/03/04 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
2020/04/14 Python
俄罗斯EPL钻石珠宝店:ЭПЛ
2019/10/22 全球购物
大学生求职简历的自我评价
2013/10/21 职场文书
制定岗位职责的原则
2013/11/08 职场文书
应用数学自荐书范文
2013/11/24 职场文书
销售员个人求职的自我评价
2014/02/10 职场文书
高等教育专业自荐信范文
2014/03/26 职场文书
竞聘报告优秀范文
2014/11/06 职场文书
重阳节主题班会
2015/08/17 职场文书
学校体育节班级口号
2015/12/25 职场文书
实习报告怎么写
2019/06/20 职场文书
Python爬虫之爬取最新更新的小说网站
2021/05/06 Python