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,
# dito for text2
text2Lines = text2.splitlines(1)
print "Lines of text2:"
for line in text2Lines:
print line,
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實現比較兩段文本不同之處的方法
本文地址: http://www.cppcns.com/jiaoben/python/125614.html
總結
以上是生活随笔為你收集整理的python 列表比较不同_python实现比较两段文本不同之处的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win10任务栏卡死如何解决
- 下一篇: python脚本根据cookies自动登