python實(shí)現(xiàn)比較兩段文本不同之處的方法
更新時(shí)間:2015年05月30日 12:39:34 作者:不吃皮蛋
這篇文章主要介紹了python實(shí)現(xiàn)比較兩段文本不同之處的方法,涉及Python針對(duì)文本與字符串的相關(guān)操作技巧,需要的朋友可以參考下
本文實(shí)例講述了python實(shí)現(xiàn)比較兩段文本不同之處的方法。分享給大家供大家參考。具體實(shí)現(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,
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python爬蟲實(shí)戰(zhàn)之使用Scrapy爬取豆瓣圖片
在用Python的urllib和BeautifulSoup寫過了很多爬蟲之后,本人決定嘗試著名的Python爬蟲框架——Scrapy.本次分享將詳細(xì)講述如何利用Scrapy來下載豆瓣名人圖片,需要的朋友可以參考下2021-06-06Python實(shí)現(xiàn)操作Redis所有類型的方法詳解
Redis作為一款高性能的NoSQL數(shù)據(jù)庫,越來越受到了廣大開發(fā)者的喜愛。本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級(jí)用法,感興趣的可以了解一下2023-04-04python開發(fā)之a(chǎn)naconda以及win7下安裝gensim的方法
這篇文章主要介紹了python開發(fā)之a(chǎn)naconda以及win7下安裝gensim的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07selenium獲取元素定位的方法總結(jié)(動(dòng)態(tài)獲取元素)
要想操作一個(gè)元素,首先應(yīng)該識(shí)別這個(gè)元素,人有各種的特征(屬性),可以通過其特征找到人,同理,界面的某個(gè)元素會(huì)有各種的特征(屬性),可以通過這個(gè)屬性找到這對(duì)象,本文給大家介紹了python?selenium獲取元素定位的8種方法,需要的朋友可以參考下2024-02-02