码迷,mamicode.com
首页 > 其他好文 > 详细

3.3.6 使用difflib.context_diff函数

时间:2015-09-28 11:50:39      阅读:433      评论:0      收藏:0      [点我收藏+]

标签:difflib   milang   python   

只想简单地比较一下文本之间是否有变化,就可以函数defflib.context_diff来实现。

例子:

#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  深圳

版权声明:本文为博主原创文章,未经博主允许不得转载。

3.3.6 使用difflib.context_diff函数

标签:difflib   milang   python   

原文地址:http://blog.csdn.net/caimouse/article/details/48782491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!