python實(shí)現(xià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使用Pillow創(chuàng)建可自定義的圖標(biāo)生成器
在本篇博客中,我們將探討如何使用?wxPython?和?Pillow?庫創(chuàng)建一個(gè)簡(jiǎn)單的圖標(biāo)生成器,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
Python實(shí)現(xiàn)批量把SVG格式轉(zhuǎn)成png、pdf格式的代碼分享
這篇文章主要介紹了Python實(shí)現(xiàn)批量把SVG格式轉(zhuǎn)成png、pdf格式的代碼分享,本文代碼需要引用一個(gè)第三方模塊cairosvg,需要的朋友可以參考下2014-08-08
Python實(shí)現(xiàn)的爬取百度文庫功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)的爬取百度文庫功能,結(jié)合實(shí)例形式分析了Python針對(duì)百度文庫的爬取、編碼轉(zhuǎn)換、文件保存等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
深入探究python中Pandas庫處理缺失數(shù)據(jù)和數(shù)據(jù)聚合
在本篇文章中,我們將深入探討Pandas庫中兩個(gè)重要的數(shù)據(jù)處理功能:處理缺失數(shù)據(jù)和數(shù)據(jù)聚合,文中有詳細(xì)的代碼示例,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-07-07
python光學(xué)仿真實(shí)現(xiàn)光線追跡折射與反射的實(shí)現(xiàn)
這篇文章主要為大家介紹了python光學(xué)仿真實(shí)現(xiàn)光線追跡折射與反射的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10
python 自動(dòng)重連wifi windows的方法
今天小編就為大家分享一篇python 自動(dòng)重連wifi windows的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12

