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读写Redis数据库操作示例
Mar 18 Python
在python的WEB框架Flask中使用多个配置文件的解决方法
Apr 18 Python
Python入门篇之列表和元组
Oct 17 Python
python机器学习之贝叶斯分类
Mar 26 Python
Python smtplib实现发送邮件功能
May 22 Python
Python3对称加密算法AES、DES3实例详解
Dec 06 Python
如何在python中写hive脚本
Nov 08 Python
python3中利用filter函数输出小于某个数的所有回文数实例
Nov 24 Python
Python unittest生成测试报告过程解析
Sep 08 Python
浅析Python 责任链设计模式
Sep 11 Python
Django创建一个后台的基本步骤记录
Oct 02 Python
Python创建文件夹与文件的快捷方法
Dec 08 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
linux下删除7天前日志的代码(php+shell)
2011/01/02 PHP
php中获得视频时间总长度的另一种方法
2011/09/15 PHP
如何使用php判断服务器是否是HTTPS连接
2013/07/05 PHP
php自定义函数截取汉字长度
2014/05/15 PHP
用js实现多域名不同文件的调用方法
2007/01/12 Javascript
js三种排序算法分享
2012/08/16 Javascript
JS对select控件option选项的增删改查示例代码
2013/10/21 Javascript
js实现文本框中焦点在最后位置
2014/03/04 Javascript
ZeroClipboard插件实现多浏览器复制功能(支持firefox、chrome、ie6)
2014/08/30 Javascript
jQuery实现可拖拽的许愿墙效果【附demo源码下载】
2016/09/14 Javascript
easyUI实现(alert)提示框自动关闭的实例代码
2016/11/07 Javascript
Javascript基础回顾之(二) js作用域
2017/01/31 Javascript
vue.js实现含搜索的多种复选框(附源码)
2017/03/23 Javascript
JavaScript实现单击网页任意位置打开新窗口与关闭窗口的方法
2017/09/21 Javascript
vue-cli3+typescript初体验小结
2019/02/28 Javascript
详解Vue 换肤方案验证
2019/08/28 Javascript
Vue如何实现验证码输入交互
2020/12/07 Vue.js
ES6 十大特性简介
2020/12/09 Javascript
Nodejs实现微信分账的示例代码
2021/01/19 NodeJs
Python松散正则表达式用法分析
2016/04/29 Python
使用Python判断质数(素数)的简单方法讲解
2016/05/05 Python
深入理解python try异常处理机制
2016/06/01 Python
详解Python的数据库操作(pymysql)
2019/04/04 Python
python 创建一维的0向量实例
2019/12/02 Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
2020/03/02 Python
python实现一个猜拳游戏
2020/04/05 Python
Python进行统计建模
2020/08/10 Python
python判断元素是否存在的实例方法
2020/09/24 Python
处理HTML5新标签的浏览器兼容版问题
2017/03/13 HTML / CSS
皮肤科医师岗位职责
2013/12/04 职场文书
个人承诺书怎么写
2014/05/24 职场文书
男性健康日的活动方案
2014/08/18 职场文书
民主评议党员自我评价材料
2014/09/18 职场文书
房产遗嘱范本
2015/08/06 职场文书
Python快速优雅的批量修改Word文档样式
2021/05/20 Python
TypeScript实用技巧 Nominal Typing名义类型详解
2022/09/23 Javascript