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生成验证码实例
Aug 21 Python
Python格式化压缩后的JS文件的方法
Mar 05 Python
Python简单实现子网掩码转换的方法
Apr 13 Python
利用python将json数据转换为csv格式的方法
Mar 22 Python
Python遍历numpy数组的实例
Apr 04 Python
高效使用Python字典的清单
Apr 04 Python
django的csrf实现过程详解
Jul 26 Python
pytorch 输出中间层特征的实例
Aug 17 Python
python多进程并行代码实例
Sep 30 Python
基于Python数据结构之递归与回溯搜索
Feb 26 Python
python能做哪些生活有趣的事情
Sep 09 Python
解决import tensorflow导致jupyter内核死亡的问题
Feb 06 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逐行输出(ob_flush与flush的组合)
2012/02/04 PHP
PHP中比较两个字符串找出第一个不同字符位置例子
2014/04/08 PHP
JavaScript 学习笔记二 字符串拼接
2010/03/28 Javascript
jQuery 事件的命名空间简单了解
2013/11/22 Javascript
js打开windows上的可执行文件示例
2014/05/27 Javascript
JSON与XML优缺点对比分析
2015/07/17 Javascript
Bootstrap响应式侧边栏改进版
2016/09/17 Javascript
Bootstrop实现多级下拉菜单功能
2016/11/24 Javascript
基于jQuery实现简单人工智能聊天室
2017/02/10 Javascript
JS实现本地存储信息的方法(基于localStorage与userData)
2017/02/18 Javascript
vue.js实现带日期星期的数字时钟功能示例
2018/08/28 Javascript
AngularJS 多指令Scope问题的解决
2018/10/25 Javascript
浅析vue 函数配置项watch及函数 $watch 源码分享
2018/11/22 Javascript
解决cordova+vue 项目打包成APK应用遇到的问题
2019/05/10 Javascript
微信小程序开发之转发分享功能
2019/10/22 Javascript
原生JS实现微信通讯录
2020/06/18 Javascript
JavaScript中的函数式编程详解
2020/08/22 Javascript
vue图片裁剪插件vue-cropper使用方法详解
2020/12/16 Vue.js
[06:11]2014DOTA2国际邀请赛 专访团结一心的VG战队
2014/07/21 DOTA
Python help()函数用法详解
2014/03/11 Python
python将每个单词按空格分开并保存到文件中
2018/03/19 Python
windows环境下tensorflow安装过程详解
2018/03/30 Python
Python切片操作深入详解
2018/07/27 Python
Anaconda+Pycharm环境下的PyTorch配置方法
2020/03/13 Python
CSS3中线性颜色渐变的一些实现方法
2015/07/14 HTML / CSS
日本食品网上商店:JaponShop.com
2017/11/28 全球购物
国际贸易专业个人职业生涯规划
2014/02/15 职场文书
法律进机关实施方案
2014/03/12 职场文书
体现团队精神的口号
2014/06/06 职场文书
教师求职自荐书
2014/06/14 职场文书
九一八事变演讲稿范文
2014/09/14 职场文书
围城读书笔记
2015/06/26 职场文书
世界各国短波电台对东亚播送时间频率表(SW)
2021/06/28 无线电
「月刊Comic Alive」2022年5月号封面公开
2022/03/21 日漫
基于Apache Hudi在Google云构建数据湖平台的思路详解
2022/04/07 Servers
CentOS7 minimal 最小化安装网络设置过程
2022/12/24 Servers