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解析发往本机的数据包示例 (解析数据包)
Jan 16 Python
python解析中国天气网的天气数据
Mar 21 Python
python检查URL是否正常访问的小技巧
Feb 25 Python
python 限制函数调用次数的实例讲解
Apr 21 Python
python async with和async for的使用
Jun 20 Python
python画图--输出指定像素点的颜色值方法
Jul 03 Python
对Python中画图时候的线类型详解
Jul 07 Python
python cv2读取rtsp实时码流按时生成连续视频文件方式
Dec 25 Python
python 弧度与角度互转实例
Apr 15 Python
python实现简单的五子棋游戏
Sep 01 Python
python中sys模块的介绍与实例
Apr 17 Python
python中出现invalid syntax报错的几种原因分析
Feb 12 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简单对象与数组的转换函数代码(php多层数组和对象的转换)
2011/05/18 PHP
ThinkPHP CURD方法之data方法详解
2014/06/18 PHP
PHP循环遍历数组的3种方法list()、each()和while总结
2014/11/19 PHP
CodeIgniter中使用Smarty3基本配置
2015/06/29 PHP
PHP抓取及分析网页的方法详解
2016/04/26 PHP
PHP MYSQL简易交互式站点开发
2016/12/27 PHP
整理的比较全的event对像在ie与firefox浏览器中的区别
2013/11/25 Javascript
jQuery异步验证用户名是否存在示例代码
2014/05/21 Javascript
JavaScript判断textarea值是否为空并给出相应提示
2014/09/04 Javascript
jQuery函数map()和each()介绍及异同点分析
2014/11/08 Javascript
node.js中的fs.renameSync方法使用说明
2014/12/16 Javascript
深入剖析JavaScript中的函数currying柯里化
2016/04/29 Javascript
基于Bootstrap的Metronic框架实现条码和二维码的生成及打印处理操作
2016/08/29 Javascript
js仿iphone秒表功能 计算平均数
2017/01/11 Javascript
详解Vue + Vuex 如何使用 vm.$nextTick
2017/11/20 Javascript
使用vue2实现购物车和地址选配功能
2018/03/29 Javascript
解决vue attr取不到属性值的问题
2018/09/18 Javascript
Vue 实现从文件中获取文本信息的方法详解
2019/10/16 Javascript
Vue-resource安装过程及使用方法解析
2020/07/21 Javascript
python计算圆周长、面积、球体体积并画出圆
2014/04/08 Python
Python values()与itervalues()的用法详解
2019/11/27 Python
python之array赋值技巧分享
2019/11/28 Python
Python文字截图识别OCR工具实例解析
2020/03/05 Python
Python基于time模块表示时间常用方法
2020/06/18 Python
python 中的命名空间,你真的了解吗?
2020/08/19 Python
大整数数相乘的问题
2012/07/22 面试题
2014教师研修学习体会
2014/07/08 职场文书
授权委托书
2014/07/31 职场文书
党的群众路线教育实践活动对照检查材料(教师)
2014/09/24 职场文书
党支部先进事迹材料
2014/12/24 职场文书
综合实践活动报告
2015/02/05 职场文书
考勤制度通知
2015/04/25 职场文书
初中语文教师研修日志
2015/11/13 职场文书
python 遍历磁盘目录的三种方法
2021/04/02 Python
详解python网络进程
2021/06/15 Python
关于mysql中string和number的转换问题
2022/06/14 MySQL