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网络编程之TCP通信实例和socketserver框架使用例子
Apr 25 Python
用Python的线程来解决生产者消费问题的示例
Apr 02 Python
python实现TCP服务器端与客户端的方法详解
Apr 30 Python
Python模拟百度登录实例详解
Jan 20 Python
python对list中的每个元素进行某种操作的方法
Jun 29 Python
Python利用递归实现文件的复制方法
Oct 27 Python
python对绑定事件的鼠标、按键的判断实例
Jul 17 Python
Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法
Aug 23 Python
详解Django配置优化方法
Nov 18 Python
python两个_多个字典合并相加的实例代码
Dec 26 Python
Python 排序最长英文单词链(列表中前一个单词末字母是下一个单词的首字母)
Dec 14 Python
Python机器学习之PCA降维算法详解
May 19 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
深入Memcache的Session数据的多服务器共享详解
2013/06/13 PHP
PHP队列用法实例
2014/11/05 PHP
PHP自带ZIP压缩、解压缩类ZipArchiv使用指南
2015/03/03 PHP
PHP实现Google plus的好友拖拽分组效果
2016/10/21 PHP
PHP 7.0新增加的特性介绍
2017/06/08 PHP
ThinkPHP5.0 图片上传生成缩略图实例代码说明
2018/06/20 PHP
JS特权方法定义作用以及与公有方法的区别
2013/03/18 Javascript
js动态创建标签示例代码
2014/06/09 Javascript
js设置控件的隐藏与显示的两种方法
2014/08/21 Javascript
js用拖动滑块来控制图片大小的方法
2015/02/27 Javascript
每天一篇javascript学习小结(RegExp对象)
2015/11/17 Javascript
解决jQuery使用JSONP时产生的错误
2015/12/02 Javascript
AngularJS学习笔记之依赖注入详解
2016/05/16 Javascript
js基于setTimeout与setInterval实现多线程
2016/06/17 Javascript
React Native中TabBarIOS的简单使用方法示例
2017/10/13 Javascript
javascript函数的节流[throttle]与防抖[debounce]
2017/11/15 Javascript
jQuery基于闭包实现的显示与隐藏div功能示例
2018/06/09 jQuery
微信小程序左右滚动公告栏效果代码实例
2019/09/16 Javascript
TypeScript的安装、使用、自动编译的实现
2020/04/10 Javascript
在Django的模板中使用认证数据的方法
2015/07/23 Python
pytorch中tensor的合并与截取方法
2018/07/26 Python
python设置环境变量的作用和实例
2019/07/09 Python
浅谈如何使用python抓取网页中的动态数据实现
2020/08/17 Python
Python绘图实现台风路径可视化代码实例
2020/10/23 Python
HTML5+CSS3实例 :canvas 模拟实现电子彩票刮刮乐代码
2016/12/30 HTML / CSS
阿联酋航空假期:Emirates Holidays
2018/03/20 全球购物
英国家用电器折扣网站:Electrical Discount UK
2018/09/17 全球购物
PHP如何调用MYSQL存储过程
2014/05/30 面试题
JSF如何进行表格处理及取值
2012/08/06 面试题
LINUX下线程,GDI类的解释
2016/12/14 面试题
应届大学生自荐信格式
2013/09/21 职场文书
护士辞职信怎么写
2015/02/27 职场文书
2016暑期社会实践新闻稿
2015/11/25 职场文书
写好求职信的技巧解密
2019/05/14 职场文书
漫改真人电影「萌系男友是燃燃的橘色」公开先导视觉图
2022/03/21 日漫
详解Golang如何优雅的终止一个服务
2022/03/21 Golang