python实现比较两段文本不同之处的方法

 更新时间:2015年05月30日 12:39:34   作者:不吃皮蛋  
这篇文章主要介绍了python实现比较两段文本不同之处的方法,涉及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,
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读取图片属性信息

    这篇文章主要介绍了如何利用python读取图片属性信息,文章围绕python读取信息相关资料展开全文,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • python使用Pillow创建可自定义的图标生成器

    python使用Pillow创建可自定义的图标生成器

    在本篇博客中,我们将探讨如何使用 wxPython 和 Pillow 库创建一个简单的图标生成器,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • Python实现批量把SVG格式转成png、pdf格式的代码分享

    Python实现批量把SVG格式转成png、pdf格式的代码分享

    这篇文章主要介绍了Python实现批量把SVG格式转成png、pdf格式的代码分享,本文代码需要引用一个第三方模块cairosvg,需要的朋友可以参考下
    2014-08-08
  • python读取word文档的方法

    python读取word文档的方法

    这篇文章主要介绍了python读取word文档的方法,实例分析了Python基于win32com操作word文档的相关技巧,需要的朋友可以参考下
    2015-05-05
  • Python实现的爬取百度文库功能示例

    Python实现的爬取百度文库功能示例

    这篇文章主要介绍了Python实现的爬取百度文库功能,结合实例形式分析了Python针对百度文库的爬取、编码转换、文件保存等相关操作技巧,需要的朋友可以参考下
    2019-02-02
  • python argparse模块传参用法实例

    python argparse模块传参用法实例

    这篇文章主要为大家介绍了python argparse模块传参用法实例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-06-06
  • python中封装token问题

    python中封装token问题

    这篇文章主要介绍了python中封装token问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-12-12
  • 深入探究python中Pandas库处理缺失数据和数据聚合

    深入探究python中Pandas库处理缺失数据和数据聚合

    在本篇文章中,我们将深入探讨Pandas库中两个重要的数据处理功能:处理缺失数据和数据聚合,文中有详细的代码示例,对我们的学习或工作有一定的帮助,需要的朋友可以参考下
    2023-07-07
  • python光学仿真实现光线追迹折射与反射的实现

    python光学仿真实现光线追迹折射与反射的实现

    这篇文章主要为大家介绍了python光学仿真实现光线追迹折射与反射的实现示例解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步
    2021-10-10
  • python 自动重连wifi windows的方法

    python 自动重连wifi windows的方法

    今天小编就为大家分享一篇python 自动重连wifi windows的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12

最新评论