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实现的二叉树算法和kmp算法实例
Apr 25 Python
跟老齐学Python之??碌某?? target=
Sep 12 Python
详解Python编程中time模块的使用
Nov 20 Python
python日志记录模块实例及改进
Feb 12 Python
Python学习pygal绘制线图代码分享
Dec 09 Python
学习Python selenium自动化网页抓取器
Jan 20 Python
详解配置Django的Celery异步之路踩坑
Nov 25 Python
python 读取竖线分隔符的文本方法
Dec 20 Python
pandas 使用均值填充缺失值列的小技巧分享
Jul 04 Python
Python 解决相对路径问题:"No such file or directory"
Jun 05 Python
一篇文章搞懂python的转义字符及用法
Sep 03 Python
Python生成九宫格图片的示例代码
Apr 14 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
十大催泪虐心动漫电影,有几部你还没看
2020/03/04 日漫
PHP 采集获取指定网址的内容
2010/01/05 PHP
Linux下CoreSeek及PHP扩展模块的安装
2012/09/23 PHP
PHP unlink与rmdir删除目录及目录下所有文件实例代码
2018/02/07 PHP
MSN消息提示类
2006/09/05 Javascript
HTML颜色选择器实现代码
2010/11/23 Javascript
纯js写的分页表格数据为json串
2014/02/18 Javascript
jQuery中extend()和fn.extend()方法详解
2015/06/03 Javascript
Windows下用PyCharm和Visual Studio开始Python编程
2015/10/26 Javascript
原生js获取iframe中dom元素--父子页面相互获取对方dom元素的方法
2016/08/05 Javascript
JQuery 选择器、DOM节点操作练习实例
2017/09/28 jQuery
JavaScript解决浮点数计算不准确问题的方法分析
2018/07/09 Javascript
这样回答继承可能面试官更满意
2019/12/10 Javascript
js实现飞机大战小游戏
2020/08/26 Javascript
nodejs中内置模块fs,path常见的用法说明
2020/11/07 NodeJs
[02:02]DOTA2英雄基础教程 斯拉达
2013/12/11 DOTA
[02:23]2014DOTA2国际邀请赛中国战队回顾
2014/08/01 DOTA
PYTHON压平嵌套列表的简单实现
2016/06/08 Python
Python 包含汉字的文件读写之每行末尾加上特定字符
2016/12/12 Python
python 爬虫出现403禁止访问错误详解
2017/03/11 Python
Windows下Anaconda的安装和简单使用方法
2018/01/04 Python
Python lambda表达式用法实例分析
2018/12/25 Python
Python读取配置文件(config.ini)以及写入配置文件
2020/04/08 Python
Pandas中DataFrame交换列顺序的方法实现
2020/12/14 Python
HTML5开发动态音频图的实现
2020/07/02 HTML / CSS
欧洲第一的摇滚和金属乐队服装网站:EMP
2017/10/26 全球购物
Nordgreen美国官网:在线购买极简主义斯堪的纳维亚手表
2019/07/24 全球购物
size?荷兰官方网站:英国高级运动鞋精品店
2020/07/24 全球购物
家长给幼儿园的表扬信
2014/01/09 职场文书
营销总经理岗位职责
2014/02/02 职场文书
行政文秘岗位职责范本
2014/02/10 职场文书
2014年公司庆元旦活动方案
2014/03/05 职场文书
信访工作经验交流材料
2014/05/23 职场文书
中小学生学籍证明
2014/10/25 职场文书
2014年超市工作总结
2014/11/19 职场文书
民事申诉状范本
2015/05/20 职场文书