本文实例讲述了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实现比较两段文本不同之处的方法

本文地址: http://www.cppcns.com/jiaoben/python/125614.html

python 列表比较不同_python实现比较两段文本不同之处的方法相关推荐

  1. python列表list元素降序排列两种方法

    python列表list元素降序排列的两种方法 sort()方法 python列表内置了一个sort()方法,可以用于为元素列表进行排序,当将默认参数reverse设置为True,sort()方法将为 ...

  2. 用python编写一个检测两段文本相似度程序

    # -*- coding:utf-8 -*- import difflib def similar(text1,text2): # 创建SequenceMatcher对象 s = difflib.Se ...

  3. 如何匹配两段文本的语义?

    喵喵喵,好久不见啦.首先很抱歉大家期待的调参手册(下)迟迟没有出稿,最近两个月连着赶了4个DDL,整个人都不好了.最近几天终于有时间赶一下未完成的稿子了.在赶DDL的时候夹着写了这篇文章,就先发布这一 ...

  4. php 两段文本对比,不同的文字显示高亮

    php 两段文本对比,不同的文字显示高亮[下面这个只能区分错误后面的..]   <?php$str1 ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggS ...

  5. python找出两个列表不同元素_python 找出两个列表的相同元素与不同元素

    python 找出两个列表的相同元素与不同元素,用了以下几种方法 1. 通过set 集合 list1 = [1,2,3] list2=[2,3,4] set1 = set(list1) set2 = ...

  6. python列表字典操作_Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结...

    创建列表 sample_list = ['a',1,('a','b')] Python 列表操作 sample_list = ['a','b',0,1,3] 得到列表中的某一个值 value_star ...

  7. python列表浅复制_Python列表的深复制和浅复制示例详解

    一.深复制与浅复制 列表是Python中自带的一种数据结构,在使用列表时,拷贝操作不可避免,下面简单讨论一下列表的深复制(拷贝)与浅复制 首先看代码: l1 = [5, 4, 3, 2, 1] # 用 ...

  8. python列表对象相同_Python中的学习列表对象,List

    List(列表) 是 Python 中使用最频繁的数据类型. 列表可以完成大多数集合类的数据结构实现.列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套). 列表是写在方括号 [ ...

  9. python列表修改函数_python 禁止函数修改列表的实现方法

    有时候,需要禁止函数修改列表.例如要对裂变进行修改操作,也要保留原来的未打印的设计列表,以供备案.为解决这个问题,可向函数传递列表的副本而不是原件:这样函数所做的任何修改都只影响副本,而丝毫不影响原件 ...

最新文章

  1. UVA 10954 - Add All
  2. Redis整合Spring Data Redis 开发key和value乱码
  3. python爬虫入门实例-Python爬虫天气预报实例详解(小白入门)
  4. GitHub 2W 星:一键生成前后端代码
  5. 细说双 11 直播背后的压测保障技术
  6. Petrozavodsk Winter Camp, Warsaw U, 2014, A The Carpet
  7. 浏览器 UC 自动添加关键字 去掉关键字
  8. PageHelper.cs(20170223)
  9. ovirt 添加存储域错误提示
  10. 深入理解jQuery插件开发(讲解jQuery开发的好文)
  11. 中南民族大学计算机图像处理实验报告,中南民族大学数字图像处理实验报告.doc...
  12. python逆时针旋转矩阵_由外向内顺时针逆时针旋转矩阵
  13. 花生壳配置域名以及公网ip
  14. vue中trigger用法
  15. python签到脚本_基于Python实现签到脚本过程解析
  16. html autocomplete 多个参数切换
  17. 七代处理器装win7_雷神ST(Intel第七代CPU)怎么一键重装win7系统
  18. 熟悉RealView MDK集成开发环境(Keil uVision4)的使用
  19. Python日常小技巧(持续更新中)
  20. 计算机网络基础知识1

热门文章

  1. python飞机大战资料-Python之游戏开发-飞机大战
  2. 经常遇到的10大C语言基础算法(珍藏版源码)
  3. 拥有属于你的“尬聊”机器人
  4. Digital System Design_VHDL设计
  5. java autovalue_Android AutoValue使用和扩展库
  6. matlab双纵坐标的绘图命令_MATLAB基础学习之绘图基本命令
  7. mysql 下一条数据_mysql 查询一条记录的下一条和上一条记录
  8. go channel 缓冲区最大限制_一起攻克面试难关:Go 面试每天一篇(第 40 天)
  9. wxpython bind自定义_wxPython的 - 如何从自定义对话框WX
  10. 团队任务3:每日立会(2018-10-25)