欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實現(xiàn)比較兩段文本不同之處的方法

 更新時間:2015年05月30日 12:39:34   作者:不吃皮蛋  
這篇文章主要介紹了python實現(xiàn)比較兩段文本不同之處的方法,涉及Python針對文本與字符串的相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了python實現(xiàn)比較兩段文本不同之處的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

# 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程序設(shè)計有所幫助。

相關(guān)文章

  • Python爬蟲實戰(zhàn)之使用Scrapy爬取豆瓣圖片

    Python爬蟲實戰(zhàn)之使用Scrapy爬取豆瓣圖片

    在用Python的urllib和BeautifulSoup寫過了很多爬蟲之后,本人決定嘗試著名的Python爬蟲框架——Scrapy.本次分享將詳細講述如何利用Scrapy來下載豆瓣名人圖片,需要的朋友可以參考下
    2021-06-06
  • python中的getter與setter你了解嗎

    python中的getter與setter你了解嗎

    這篇文章主要為大家詳細介紹了python中的getter與setter,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Python實現(xiàn)操作Redis所有類型的方法詳解

    Python實現(xiàn)操作Redis所有類型的方法詳解

    Redis作為一款高性能的NoSQL數(shù)據(jù)庫,越來越受到了廣大開發(fā)者的喜愛。本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級用法,感興趣的可以了解一下
    2023-04-04
  • python中partial()基礎(chǔ)用法說明

    python中partial()基礎(chǔ)用法說明

    這篇文章主要給大家介紹了關(guān)于python中partial()基礎(chǔ)用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用python具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-12-12
  • python偏函數(shù)partial用法

    python偏函數(shù)partial用法

    這篇文章要給大家分享得是python偏函數(shù)partial用法,主要介紹什么是偏函數(shù)partial、偏函數(shù)的作用、偏函數(shù)的語法及案例詳情,需要的朋友可以參考一下文章得具體詳解,希望對你有所幫助
    2021-10-10
  • python開發(fā)之a(chǎn)naconda以及win7下安裝gensim的方法

    python開發(fā)之a(chǎn)naconda以及win7下安裝gensim的方法

    這篇文章主要介紹了python開發(fā)之a(chǎn)naconda以及win7下安裝gensim的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • python爬取cnvd漏洞庫信息的實例

    python爬取cnvd漏洞庫信息的實例

    今天小編就為大家分享一篇python爬取cnvd漏洞庫信息的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-02-02
  • selenium獲取元素定位的方法總結(jié)(動態(tài)獲取元素)

    selenium獲取元素定位的方法總結(jié)(動態(tài)獲取元素)

    要想操作一個元素,首先應該識別這個元素,人有各種的特征(屬性),可以通過其特征找到人,同理,界面的某個元素會有各種的特征(屬性),可以通過這個屬性找到這對象,本文給大家介紹了python?selenium獲取元素定位的8種方法,需要的朋友可以參考下
    2024-02-02
  • python文件操作之批量修改文件后綴名的方法

    python文件操作之批量修改文件后綴名的方法

    這篇文章主要介紹了python文件操作之批量修改文件后綴名,需要的朋友可以參考下
    2018-08-08
  • Python urlopen()函數(shù) 示例分享

    Python urlopen()函數(shù) 示例分享

    urlopen(url, data=None, proxies=None) 即創(chuàng)建一個表示遠程url的類文件對象,然后像本地文件一樣操作這個類文件對象來獲取遠程數(shù)據(jù)。參數(shù)url表示遠程數(shù)據(jù)的路徑,一般是網(wǎng)址;參數(shù)data表示以post方式提交到url的數(shù)據(jù);參數(shù)proxies用于設(shè)置代理。
    2014-06-06

最新評論