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中__getattr__和 __getattribute__获取属性的用法
Jun 21 Python
Django+Ajax+jQuery实现网页动态更新的实例
May 28 Python
Python实现获取邮箱内容并解析的方法示例
Jun 16 Python
Centos下实现安装Python3.6和Python2共存
Aug 15 Python
python 使用re.search()筛选后 选取部分结果的方法
Nov 28 Python
python实现蒙特卡罗方法教程
Jan 28 Python
Python使用while循环花式打印乘法表
Jan 28 Python
python 公共方法汇总解析
Sep 16 Python
Python vtk读取并显示dicom文件示例
Jan 13 Python
PyTorch加载自己的数据集实例详解
Mar 18 Python
在PyCharm中安装PaddlePaddle的方法
Feb 05 Python
matplotlib之pyplot模块坐标轴标签设置使用(xlabel()、ylabel())
Feb 22 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中英混合字符串截取函数代码
2011/07/17 PHP
php魔术变量用法实例详解
2014/11/13 PHP
php使用simplexml_load_file加载XML文件并显示XML的方法
2015/03/19 PHP
php使用正则表达式去掉html中的注释方法
2016/11/03 PHP
PHP实现类似题库抽题效果
2018/08/16 PHP
accesskey 提交
2006/06/26 Javascript
原生Js与jquery的多组处理, 仅展开一个区块的折叠效果
2011/01/09 Javascript
一行代码实现纯数据json对象的深度克隆实现思路
2013/01/09 Javascript
自己编写的支持Ajax验证的JS表单验证插件
2015/05/15 Javascript
解决JS请求服务器gbk文件乱码的问题
2015/10/16 Javascript
bootstrap学习笔记之初识bootstrap
2016/06/21 Javascript
基于BootStrap multiselect.js实现的下拉框联动效果
2017/07/28 Javascript
详解nodejs解压版安装和配置(带有搭建前端项目脚手架)
2018/12/06 NodeJs
React 实现拖拽功能的示例代码
2019/01/06 Javascript
微信小程序 自定义复选框实现代码实例
2019/09/04 Javascript
js实现橱窗展示效果
2020/01/11 Javascript
python通过自定义isnumber函数判断字符串是否为数字的方法
2015/04/23 Python
Python解析json文件相关知识学习
2016/03/01 Python
python+matplotlib实现动态绘制图片实例代码(交互式绘图)
2018/01/20 Python
python 去除二维数组/二维列表中的重复行方法
2019/01/23 Python
代码详解django中数据库设置
2019/01/28 Python
python 应用之Pycharm 新建模板默认添加编码格式-作者-时间等信息【推荐】
2019/06/17 Python
利用python开发app实战的方法
2019/07/09 Python
python解析命令行参数的三种方法详解
2019/11/29 Python
python上传时包含boundary时的解决方法
2020/04/08 Python
matplotlib 曲线图 和 折线图 plt.plot()实例
2020/04/17 Python
HTML5+CSS设置浮动却没有动反而在中间且错行的问题
2020/05/26 HTML / CSS
美国时尚在线:Showpo
2017/09/08 全球购物
JACK & JONES英国官方网站:欧洲领先的男装生产商
2017/09/27 全球购物
将世界上最美丽的摄影作品转化为艺术作品:Photos.com
2017/11/28 全球购物
团员学习总结的自我评价范文
2013/10/14 职场文书
生物技术毕业生自荐信
2013/10/23 职场文书
最新大学生创业计划书写作攻略
2014/04/02 职场文书
银行会计主管岗位职责
2014/10/01 职场文书
乱丢垃圾袋检讨书
2014/10/08 职场文书
慰问信的写作格式及范文!
2019/06/24 职场文书