3.3.6 使用difflib.context_diff函数
2015-09-28 11:28
337 查看
只想简单地比较一下文本之间是否有变化,就可以函数defflib.context_diff来实现。
例子:
结果输出如下:
*** fromfile fromfiledate
--- tofile tofiledate
***************
*** 1,5 ****
1. Beautiful is better than ugly.
! 2. Explicit is better than implicit.
! 3. Simple is better than complex.
! 4. Complex is better than complicated.
--- 1,6 ----
1. Beautiful is better than ugly.
! 3. Simple is better than complex.
! 4. Complicated is better than complex.
! 5. Flat is better than nested.
! 0. test
在每行头有!的,就表示此行已经作出改变。
蔡军生 微信号:shenzhencai 深圳
例子:
#python3.4.3 import difflib text1 = ''' 1. Beautiful is better than ugly. 2. Explicit is better than implicit. 3. Simple is better than complex. 4. Complex is better than complicated. '''.splitlines(keepends=True) text2 = ''' 1. Beautiful is better than ugly. 3. Simple is better than complex. 4. Complicated is better than complex. 5. Flat is better than nested. 0. test '''.splitlines(keepends=True) for line in difflib.context_diff(text1, text2, 'fromfile', 'tofile', 'fromfiledate', 'tofiledate', 1, '\n'): print(line)
结果输出如下:
*** fromfile fromfiledate
--- tofile tofiledate
***************
*** 1,5 ****
1. Beautiful is better than ugly.
! 2. Explicit is better than implicit.
! 3. Simple is better than complex.
! 4. Complex is better than complicated.
--- 1,6 ----
1. Beautiful is better than ugly.
! 3. Simple is better than complex.
! 4. Complicated is better than complex.
! 5. Flat is better than nested.
! 0. test
在每行头有!的,就表示此行已经作出改变。
蔡军生 微信号:shenzhencai 深圳
相关文章推荐
- OkHttp使用教程
- Jquery 学习笔记一
- Spring 使用context:annotation-config的设置
- 三维GIS在交通应急上的应用
- html5 Api 浏览器全屏使用心得
- codevs 1380 没有上司的舞会|树形动规
- vim显示行号、语法高亮、自动缩进的设置
- 201509281125_《为什么移动app会很慢的深度分析(摘自司徒正美博客园文章)》
- SharePoint自动化系列——Error features自动deactivate
- Java 比较两个日期的方法
- View的绘制流程
- 三层标签pop
- nginx 配置 ssl 模块支持 https
- C#学习之事件
- Leetcode81: Search Insert Position
- DevExpress v15.1.5发布[附下载]
- iphone media query
- MVC笔记3:JQuery AutoComplete组件
- C# EPL USB 指令打印
- ListCtrl虚拟列表---加载大数据