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爬取可用的代理IP
Aug 18 Python
Django rest framework工具包简单用法示例
Jul 20 Python
python集合比较(交集,并集,差集)方法详解
Sep 13 Python
Python3中关于cookie的创建与保存
Oct 21 Python
python的json中方法及jsonpath模块用法分析
Dec 06 Python
pytorch ImageFolder的覆写实例
Feb 20 Python
Python 将 QQ 好友头像生成祝福语的实现代码
May 03 Python
Python select及selectors模块概念用法详解
Jun 22 Python
解决django migrate报错ORA-02000: missing ALWAYS keyword
Jul 02 Python
python tkinter的消息框模块(messagebox,simpledialog)
Nov 07 Python
Django中ORM的基本使用教程
Dec 22 Python
python 实现IP子网计算
Feb 18 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下使用无限生命期Session的方法
2007/03/16 PHP
PHP人民币金额转大写实例代码
2015/10/02 PHP
jquery.ui.draggable中文文档
2009/11/24 Javascript
jquery获取input的value问题说明
2010/08/19 Javascript
javascript利用初始化数据装配模版的实现代码
2010/11/17 Javascript
JavaScript高级程序设计 读书笔记之十 本地对象Date日期
2012/02/27 Javascript
jQuery选择器中含有空格的使用示例及注意事项
2013/08/25 Javascript
JS实现单行文字不间断向上滚动的方法
2015/01/29 Javascript
js获取时间精确到秒(年月日)
2016/03/16 Javascript
jQuery插件echarts实现的多折线图效果示例【附demo源码下载】
2017/03/04 Javascript
Flask中获取小程序Request数据的两种方法
2017/05/12 Javascript
jQuery实现简单的计时器功能实例分析
2017/08/29 jQuery
基于ES6作用域和解构赋值详解
2017/11/03 Javascript
微信小程序用户信息encryptedData详解
2018/08/24 Javascript
详解@Vue/Cli 3 Invalid Host header 错误解决办法
2019/01/02 Javascript
详解Vue前端对axios的封装和使用
2019/04/01 Javascript
微信小程序获取用户信息并保存登录状态详解
2019/05/10 Javascript
详解在vue-cli3.0中自定css、js和图片的打包路径
2019/08/26 Javascript
python益智游戏计算汉诺塔问题示例
2014/03/05 Python
Python判断变量是否已经定义的方法
2014/08/18 Python
实例Python处理XML文件的方法
2015/08/31 Python
Python文件操作,open读写文件,追加文本内容实例
2016/12/14 Python
Sanic框架应用部署方法详解
2018/07/18 Python
Python3.5文件读与写操作经典实例详解
2019/05/01 Python
wxpython实现按钮切换界面的方法
2019/11/19 Python
如何将json数据转换为python数据
2020/09/04 Python
python使用正则表达式匹配txt特定字符串(有换行)
2020/12/09 Python
CSS3 box-sizing属性
2009/04/17 HTML / CSS
从一次项目重构说起CSS3自定义变量在项目的使用方法
2021/03/01 HTML / CSS
HTML5实现获取地理位置信息并定位功能
2015/04/25 HTML / CSS
管理科学大学生求职信
2013/11/13 职场文书
如何写一份好的自荐信
2014/01/02 职场文书
影视广告专业求职信
2014/09/02 职场文书
毕业设计论文评语
2014/12/31 职场文书
银行保安拾金不昧表扬稿
2015/05/05 职场文书
社会实践单位意见
2015/06/05 职场文书