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 可爱的大小写
Sep 06 Python
python实现从一组颜色中找出与给定颜色最接近颜色的方法
Mar 19 Python
python在线编译器的简单原理及简单实现代码
Feb 02 Python
Python实现的径向基(RBF)神经网络示例
Feb 06 Python
VSCode下配置python调试运行环境的方法
Apr 06 Python
python-tkinter之按钮的使用,开关方法
Jun 11 Python
基于pandas中expand的作用详解
Dec 17 Python
对Pytorch中Tensor的各种池化操作解析
Jan 03 Python
Django 项目通过加载不同env文件来区分不同环境
Feb 17 Python
PyCharm 专业版安装图文教程
Feb 20 Python
python 回溯法模板详解
Feb 26 Python
Python3.9.1中使用split()的处理方法(推荐)
Feb 07 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
如何修改和添加Apache的默认站点目录
2013/07/05 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
2014/08/25 PHP
php实现的css文件背景图片下载器代码
2014/11/11 PHP
PHP 根据key 给二维数组分组
2016/12/09 PHP
PHP使用Nginx实现反向代理
2017/09/20 PHP
laravel 去掉index.php伪静态的操作方法
2019/10/12 PHP
PHP使用gearman进行异步的邮件或短信发送操作详解
2020/02/27 PHP
简单的js分页脚本
2009/05/21 Javascript
javascript 获取url参数和script标签中获取url参数函数代码
2010/01/22 Javascript
一个简单的瀑布流效果(主体形式自写)
2013/05/27 Javascript
使用firebug进行调试javascript的示例
2013/12/16 Javascript
JavaScript实现点击文字切换登录窗口的方法
2015/05/11 Javascript
JSP基于Bootstrap分页显示实例解析
2016/06/12 Javascript
jQuery插件简单学习实例教程
2016/07/01 Javascript
js中常用的Tab切换效果(推荐)
2016/08/30 Javascript
表单元素值获取方式js及java方式的简单实例
2016/10/15 Javascript
JS实现图片垂直居中显示小结
2016/12/13 Javascript
Angular指令之restict匹配模式的详解
2017/07/27 Javascript
基于Bootstrap框架菜鸟入门教程(推荐)
2017/09/17 Javascript
vue中如何动态绑定图片,vue中通过data返回图片路径的方法
2018/02/07 Javascript
微信小程序实现弹出菜单功能
2018/06/12 Javascript
在vue中使用防抖和节流,防止重复点击或重复上拉加载实例
2019/11/13 Javascript
在vscode 中设置 vue模板内容的方法
2020/09/02 Javascript
python3实现基于用户的协同过滤
2018/05/31 Python
使用urllib库的urlretrieve()方法下载网络文件到本地的方法
2018/12/19 Python
Python爬虫设置代理IP(图文)
2018/12/23 Python
Python3.5文件修改操作实例分析
2019/05/01 Python
python实现电子书翻页小程序
2019/07/23 Python
使用Keras加载含有自定义层或函数的模型操作
2020/06/10 Python
python如何获得list或numpy数组中最大元素对应的索引
2020/11/16 Python
总结python 三种常见的内存泄漏场景
2020/11/20 Python
英语专业学生个人求职信
2014/01/28 职场文书
ktv好的活动方案
2014/08/15 职场文书
竞选班长演讲稿500字
2014/08/22 职场文书
百年校庆感言
2015/08/01 职场文书
家电创业计划书
2019/08/05 职场文书