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中使用enumerate函数遍历元素实例
Jun 16 Python
web.py获取上传文件名的正确方法
Aug 26 Python
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
Jan 17 Python
python实现内存监控系统
Mar 07 Python
python爬取cnvd漏洞库信息的实例
Feb 14 Python
Python爬虫动态ip代理防止被封的方法
Jul 07 Python
Python 模拟动态产生字母验证码图片功能
Dec 24 Python
Python super()函数使用及多重继承
May 06 Python
如何使用python的ctypes调用医保中心的dll动态库下载医保中心的账单
May 24 Python
Python参数传递及收集机制原理解析
Jun 05 Python
python基础之类方法和静态方法
Oct 24 Python
Python使用永中文档转换服务
May 06 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 截取字符串 分别适合GB2312和UTF8编码情况
2009/02/12 PHP
新浪微博API开发简介之用户授权(PHP基础篇)
2011/09/25 PHP
php实现图形显示Ip地址的代码及注释
2014/01/20 PHP
一些mootools的学习资源
2010/02/07 Javascript
DD_belatedPNG,IE6下PNG透明解决方案(国外)
2010/12/06 Javascript
PHP PDO操作总结
2014/11/17 Javascript
javascript操作符"!~"详解
2015/02/10 Javascript
使用jQuery实现图片遮罩半透明坠落遮挡
2015/03/16 Javascript
javascript实现图片跟随鼠标移动效果的方法
2015/05/13 Javascript
超赞的动手创建JavaScript框架的详细教程
2015/06/30 Javascript
jQuery层级选择器_动力节点节点Java学院整理
2017/07/04 jQuery
微信小程序注册60s倒计时功能 使用JS实现注册60s倒计时功能
2017/08/16 Javascript
AngularJS实现表单验证功能详解
2017/10/12 Javascript
angular1配合gulp和bower的使用教程
2018/01/19 Javascript
Vue的轮播图组件实现方法
2018/03/03 Javascript
详解基于Wepy开发小程序插件(推荐)
2019/08/01 Javascript
vue 实现websocket发送消息并实时接收消息
2019/12/09 Javascript
微信小程序登陆注册功能的实现代码
2019/12/10 Javascript
JavaScript中的类型检查
2020/02/03 Javascript
JS使用setInterval计时器实现挑战10秒
2020/11/08 Javascript
Python遍历目录中的所有文件的方法
2016/07/08 Python
python实现批量修改文件名代码
2017/09/10 Python
python爬虫简单的添加代理进行访问的实现代码
2019/04/04 Python
python 利用toapi库自动生成api
2020/10/19 Python
详解CSS透明opacity和IE各版本透明度滤镜filter的最准确用法
2016/12/20 HTML / CSS
CSS3贝塞尔曲线示例:创建链接悬停动画效果
2020/11/19 HTML / CSS
阿迪达斯芬兰官方网站:adidas芬兰
2017/01/30 全球购物
英国手机零售商:Metrofone
2019/03/18 全球购物
eHarmony英国:全球领先的认真恋爱约会平台之一
2020/11/16 全球购物
寒假思想汇报
2014/01/10 职场文书
党的群众路线教育实践活动对照检查材料
2014/09/22 职场文书
2015年感恩节演讲稿(优选篇)
2015/03/20 职场文书
2015年乡镇流动人口工作总结
2015/05/12 职场文书
python内置进制转换函数的操作
2021/06/02 Python
MySQL面试题讲解之如何设置Hash索引
2021/11/01 MySQL
彻底解决MySQL使用中文乱码的方法
2022/01/22 MySQL