比較并找出兩個文本內(nèi)容的不同
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、韶關(guān)網(wǎng)絡(luò)推廣、小程序開發(fā)、韶關(guān)網(wǎng)絡(luò)營銷、韶關(guān)企業(yè)策劃、韶關(guān)品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供韶關(guān)建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:www.chinadenli.net
import difflib
text1="""1. Beautiful is better than ugly.
2. Explicit is better than implicit.
3. Simple is better than complex.
4. Complex is better than complicated.
""".splitlines(keepends=True)
text2="""1. Beautiful is better than ugly.
3.   Simple is better than complex.
4. Complicated is better than complex.
5. Flat is better than nested.
""".splitlines(keepends=True)
#第一種方式:
d = difflib.Differ()
result = list(d.compare(text1,text2))
result = ''.join(result)
print(result)
"""
運(yùn)行結(jié)果以文本行方式顯示,不容易理解
"""
#第二種方式:
d = difflib.HtmlDiff()
with open('diffirent.html','w') as f:
    f.write(d.make_file(text1,text2))
"""
生成一個html文件,用瀏覽器打開,會顯示兩個文本內(nèi)容的不同之處
"""第一種方式:
第二種方式:
直接比較兩個文件:
import difflib
file1 = '/etc/passwd'
file2 = '/tmp/passwd'
with open(file1) as f1,open(file2) as f2:
    text1 = f1.readlines()
    text2 = f2.readlines()
d = difflib.HtmlDiff()
with open('passwd.html','w') as f:
    f.write(d.make_file(text1,text2))

                名稱欄目:Python一起來找茬—difflib模塊
                
                當(dāng)前URL:http://www.chinadenli.net/article14/igpgde.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)