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程序设计有所帮助。


猜你喜欢
- FlaskFlask是什么?Flask是一个使用 Python 编写的轻量级 Web 应用框架, 让我们可以使用Python语言快速搭建We
- vue-router切换组件时使组件不销毁默认地,使用vue-router切换组件时,离开的组件会被销毁,新进入地组件会创建。那么,有时候,
- 前言ES5中新增的不少东西,了解之对我们写JavaScript会有不少帮助,下面主要介绍了ES5中的Array对象,需要的朋友们一起来看看吧
- 一、简单介绍flask中的wtformsWTForms是一个支持多个web框架的form组件,主要用于对用户请求数据进行验证。安装:pip3
- 请问如何用OleDbDataAdapter来对数据库进行删除、修改和添加?OleDbDataAdapter是DataSet和数据源之间建立联
- 前言Python可以操作Excel的模块不止一种,我习惯使用的写入模块是xlwt(一般都是读写模块分开的)python中使用xlwt操作ex
- create proc sp_PublicTurnPageWebSite( @TBName nvarchar(
- 前言innodb_data_file_path用来指定innodb tablespace文件,如果我们不在My.cnf文件中指定innodb
- Pygame的Draw绘图Pygame 中提供了一个draw模块用来绘制一些简单的图形状,比如矩形、多边形、圆形、直线、弧线等。pygame
- 微信应用号(微信公众平台小程序,「应用号」的新称呼)终于来了!开源中国社区的博卡君通宵吐血赶稿写出的微信公众平台应用号开发教程!大家赶紧来学
- 如何使用migrations的使用非常简单: 修改model, 比如增加field, 然后运行python manager.py makem
- 之所以说”使用”而不是”实现”,是因为python的相关类库已经帮我们实现了具体算法,而我们只要学会使用就可以了。随着对技术的逐渐掌握及积累
- <!DOCTYPE html PUBLIC "-//W3C//DTD X
- 1、数据库--所有数据库的大小 exec sp_helpdb --所有数据库的状态 sel
- Random库主要包含返回随机数的函数,主要用于普通的随机数生成的程序,如果对随机性有特殊要求,比如加密等,可以用函数os.urandom(
- 光线追迹得益于计算机的计算的能力,通过追踪具有代表性的光线的传播轨迹,可以更加精确地描述光学系统的性能,光线追迹方法也因此大展其能,诸如Ze
- 【历史背景】岁月更迭中我已经从事MySQL-DBA这个工作三个年头,见证MySQL从“基本可用”,“边缘系统可以用MySQL”,“哦操!你怎
- 使用pycharm创建新项目,使用虚拟环境,但是进入到项目的cainiao_guoguo_health\venv\Scripts目录启动虚拟
- Python 实现tuple和list的转换1.list列表转换为tuple元组temp_list = [1,2,3,4,5]print(t
- DATEPART() 函数用于返回日期/时间的单独部分,比如年、月、日、小时、分钟等等。DATEDIFF() 函数返回两个日期之间的时间差。