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 Trie树实现字典排序
Mar 28 Python
python获得linux下所有挂载点(mount points)的方法
Apr 29 Python
深入理解python中的浅拷贝和深拷贝
May 30 Python
Python错误提示:[Errno 24] Too many open files的分析与解决
Feb 16 Python
Python对象属性自动更新操作示例
Jun 15 Python
Django forms组件的使用教程
Oct 08 Python
pip指定python位置安装软件包的方法
Jul 12 Python
获取Pytorch中间某一层权重或者特征的例子
Aug 17 Python
Python字符串大小写转换拼接删除空白
Sep 19 Python
python 中的paramiko模块简介及安装过程
Feb 29 Python
python两个list[]相加的实现方法
Sep 23 Python
使用python-cv2实现Harr+Adaboost人脸识别的示例
Oct 27 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留言本实例代码
2010/05/09 PHP
PHP 第二节 数据类型之数值型
2012/04/28 PHP
PHP仿qq空间或朋友圈发布动态、评论动态、回复评论、删除动态或评论的功能(上)
2017/05/26 PHP
PHP实现单条sql执行多个数据的insert语句方法
2019/10/11 PHP
动态控制Table的js代码
2007/03/07 Javascript
JavaScript 对话框和状态栏使用说明
2009/10/25 Javascript
HTML页面滚动时获取离页面顶部的距离2种实现方法
2013/09/05 Javascript
jQuery向上遍历DOM树之parents(),parent(),closest()之间的区别
2013/12/02 Javascript
angularJS 中$attrs方法使用指南
2015/02/09 Javascript
jquery实现不包含当前项的选择器实例
2015/06/25 Javascript
jQuery实现隔行变色的方法分析(对比原生JS)
2016/11/18 Javascript
Bootstarp 基础教程之表单部分实例代码
2017/02/03 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
Vuex模块化应用实践示例
2020/02/03 Javascript
vue中路由跳转不计入history的操作
2020/09/21 Javascript
解决Antd Table表头加Icon和气泡提示的坑
2020/11/17 Javascript
使用AutoJs实现微信抢红包的代码
2020/12/31 Javascript
[00:36]TI7不朽珍藏III——斯温不朽展示
2017/07/15 DOTA
python使用os.listdir和os.walk获得文件的路径的方法
2017/12/16 Python
NumPy.npy与pandas DataFrame的实例讲解
2018/07/09 Python
Django使用中间键实现csrf认证详解
2019/07/22 Python
keras中模型训练class_weight,sample_weight区别说明
2020/05/23 Python
CSS3 transform的skew属性值图文详解
2014/07/21 HTML / CSS
Reebonz中国官网:新加坡奢侈品购物网站
2017/03/17 全球购物
蒙蒂塞罗商店:Monticello Shop
2018/11/25 全球购物
法国二手MacBook销售网站:Okamac
2019/03/18 全球购物
英国最大的笔记本电脑直销专家:Laptops Direct
2019/07/20 全球购物
阿迪达斯新加坡官方网站:adidas新加坡
2019/12/06 全球购物
演讲稿怎么写才完美
2014/01/02 职场文书
校长就职演讲稿
2014/01/06 职场文书
优秀教师演讲稿
2014/05/06 职场文书
新学期开学标语
2014/06/30 职场文书
2014幼儿园班主任工作总结
2014/12/04 职场文书
幼儿园教师教育随笔
2015/08/14 职场文书
预备党员的思想汇报,你真的会写吗?
2019/06/28 职场文书
Python selenium的这三种等待方式一定要会!
2021/06/10 Python