python实现比较两段文本不同之处的方法
作者:不吃皮蛋 发布时间:2021-01-29 17:53:43
标签:python,比较,文本
本文实例讲述了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,
# dito for text2
text2Lines = text2.splitlines(1)
print "Lines of text2:"
for line in text2Lines:
print line,
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程序设计有所帮助。
0
投稿
猜你喜欢
- 前言前两篇我们分别爬取了糗事百科和妹子图网站,学习了 Requests, Beautiful Soup 的基本使用。不过前两篇都是从静态 H
- 说实在的个人对游戏并没有多大的兴趣,但唯独对暴雪的Diablo系列很有感情,去年年初开始玩Diablo3,断断续续,感觉最麻烦的是选择技能,
- 用了很久的os.path,今天发现竟然还有这么好用的库,记录下来以便使用。1.调用库from pathlib import2.创建Path对
- 一、前言在写业务代码时候,有许多场景需要重试某块业务逻辑,例如网络请求、购物下单等,希望发生异常的时候多重试几次。本文分享如何利用Pytho
- 首先先定义一个list,将其转存为csv文件,看将会报什么错误list=[[1,2,3],[4,5,6],[7,9,9]]list.to_c
- 模块导入方式: import osos模块是Python标准库中的一个用于访问操作系统相关功能的模块,os模块提供了一种可移植的使
- Update 语句Update 语句用于修改表中的数据。语法:UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值P
- 链表链表(linked list)是由一组被称为结点的数据元素组成的数据结构,每个结点都包含结点本身的信息和指向下一个结点的地址。由于每个结
- Pytorch 多分类模型绘制 ROC, PR 曲线(代码 亲测 可用)ROC曲线示例代码import torchimport torch.
- 本文实例讲述了python网络编程之读取网站根目录的方法,分享给大家供大家参考。具体实现方法如下:import socket, sys po
- 从2004年开始,我开始进入雅虎的异常表现小组。我们是一个很小的队伍,专门针对雅虎的产品进行质量检测和改进,我作为一个后端工程师,现在却开始
- python在不同层级目录import模块的方法使用python进行程序编写时,经常会调用不同目录下的模块及函数。本篇博客针对常见的模块调用
- 1. assert函数说明:Assert statements are a convenient way to insert debuggi
- 突然想到了之前一直没留意的for循环中开goroutine的执行顺序问题,就找了段代码试了试,试了几次后发现几个有意思的地方,我暂时没有精力
- 一、介绍make_blobs() 是 sklearn.datasets中的一个函数。主要是产生聚类数据集,产生一个数据集和相应的标签。函数的
- 1.将以下代码加入到HEML的<body></body>之间<SCRIPT language=jav
- 前言本文代码基于 python3.6 和 pygame1.9.4。俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一
- SQL Server所谓的分布式查询(Distributed Query)是能够访问存放在同一部计算机或不同计算机上的SQL Server或
- 实例一:题目:有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?程序分析:可填在百位、十位、个位的数字都是1、
- 很多网站现在都有使用QQ作为在线客服工具,我们点击它可以很方便的和网站人员联系,本站为你整理了在网站上使用QQ在线客服的代码,共13种风格,