python实现比较两段文本不同之处的方法


Posted in Python onMay 30, 2015

本文实例讲述了python实现比较两段文本不同之处的方法。分享给大家供大家参考。具体实现方法如下:

# find the difference between two texts
# tested with Python24  vegaseat 6/2/2005
import difflib
text1 = """The World's Shortest Books:
Human Rights Advances in China
"My Plan to Find the Real Killers" by OJ Simpson
"Strom Thurmond: Intelligent Quotes"
America's Most Popular Lawyers
Career Opportunities for History Majors
Different Ways to Spell "Bob"
Dr. Kevorkian's Collection of Motivational Speeches
Spotted Owl Recipes by the EPA
The Engineer's Guide to Fashion
Ralph Nader's List of Pleasures
"""
text2 = """The World's Shortest Books:
Human Rights Advances in China
"My Plan to Find the Real Killers" by OJ Simpson
"Strom Thurmond: Intelligent Quotes"
America's Most Popular Lawyers
Career Opportunities for History Majors
Different Ways to Sell "Bob"
Dr. Kevorkian's Collection of Motivational Speeches
Spotted Owl Recipes by the EPA
The Engineer's Guide to Passion
Ralph Nader's List of Pleasures
"""
# create a list of lines in text1
text1Lines = text1.splitlines(1)
print "Lines of text1:"
for line in text1Lines:
 print line,
print
# dito for text2
text2Lines = text2.splitlines(1)
print "Lines of text2:"
for line in text2Lines:
 print line,
print 
diffInstance = difflib.Differ()
diffList = list(diffInstance.compare(text1Lines, text2Lines))
print '-'*50
print "Lines different in text1 from text2:"
for line in diffList:
 if line[0] == '-':
  print line,

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
python数组过滤实现方法
Jul 27 Python
浅谈Python peewee 使用经验
Oct 20 Python
快速了解python leveldb
Jan 18 Python
python爬虫之线程池和进程池功能与用法详解
Aug 02 Python
python使用minimax算法实现五子棋
Jul 29 Python
应用OpenCV和Python进行SIFT算法的实现详解
Aug 21 Python
python 进程的几种创建方式详解
Aug 29 Python
Python Tornado核心及相关原理详解
Jun 24 Python
Django ModelForm组件原理及用法详解
Oct 12 Python
python模拟点击玩游戏的实例讲解
Nov 26 Python
Python实现王者荣耀自动刷金币的完整步骤
Jan 22 Python
深入解析NumPy中的Broadcasting广播机制
May 30 Python
python统计文本文件内单词数量的方法
May 30 #Python
python使用win32com库播放mp3文件的方法
May 30 #Python
基于wxpython开发的简单gui计算器实例
May 30 #Python
python图像处理之镜像实现方法
May 30 #Python
python图像处理之反色实现方法
May 30 #Python
python中字典(Dictionary)用法实例详解
May 30 #Python
python集合用法实例分析
May 30 #Python
You might like
PHP stristr() 函数(不区分大小写的字符串查找)
2010/06/03 PHP
关于ob_get_contents(),ob_end_clean(),ob_start(),的具体用法详解
2013/06/24 PHP
Laravel实现ORM带条件搜索分页
2019/10/24 PHP
理解Javascript_13_执行模型详解
2010/10/20 Javascript
基于JQUERY的多级联动代码
2012/01/24 Javascript
ASP.NET中AJAX 调用实例代码
2012/05/03 Javascript
模拟电子签章盖章效果的jQuery插件源码
2013/06/24 Javascript
jQuery中filter()方法用法实例
2015/01/06 Javascript
jQuery简单实现验证邮箱格式
2015/07/15 Javascript
第十篇BootStrap轮播插件使用详解
2016/06/21 Javascript
JS实现点击表头表格自动排序(含数字、字符串、日期)
2017/01/22 Javascript
微信小程序获取手机系统信息的方法【附源码下载】
2017/12/07 Javascript
微信小程序实现图片预览功能
2018/01/31 Javascript
React 组件中的 bind(this)示例代码
2018/09/16 Javascript
Vue js 的生命周期(看了就懂)(推荐)
2019/03/29 Javascript
vue 使用 canvas 实现手写电子签名
2020/03/06 Javascript
Vue动态加载图片在跨域时无法显示的问题及解决方法
2020/03/10 Javascript
vue实现一个6个输入框的验证码输入组件功能的实例代码
2020/06/29 Javascript
python删除某个字符
2018/03/19 Python
对python while循环和双重循环的实例详解
2019/08/23 Python
Python pandas实现excel工作表合并功能详解
2019/08/29 Python
python jenkins 打包构建代码的示例代码
2019/11/29 Python
关于numpy中eye和identity的区别详解
2019/11/29 Python
jupyter note 实现将数据保存为word
2020/04/14 Python
解决pycharm编辑区显示yaml文件层级结构遇中文乱码问题
2020/04/27 Python
Keras 切换后端方式(Theano和TensorFlow)
2020/06/19 Python
python实现图片转换成素描和漫画格式
2020/08/19 Python
python 监控logcat关键字功能
2020/09/04 Python
Python实例方法、类方法、静态方法区别详解
2020/09/05 Python
HTML5制作酷炫音频播放器插件图文教程
2014/12/30 HTML / CSS
股指期货心得体会
2014/09/10 职场文书
在人间读书笔记
2015/06/30 职场文书
活动宣传稿范文
2015/07/23 职场文书
环保主题班会教案
2015/08/13 职场文书
维护民族团结心得体会2016
2016/01/15 职场文书
python3中apply函数和lambda函数的使用详解
2022/02/28 Python