1. 程式人生 > >python比較兩個字串的不同

python比較兩個字串的不同

使用difflib庫來比較兩個字串,並標記出不同的地方

import difflib

url1="http://service.library.mtime.com/Movie.api?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Library.Services&Ajax_CallBackMethod=GetMovieOverviewRating&Ajax_CrossDomain=1&Ajax_RequestUrl=http%3A%2F%2Fmovie.mtime.com%2F232316%2F&t=20185259461767462&Ajax_CallBackArgument0=232316"
url2="http://service.library.mtime.com/Movie.api?Ajax_CallBack=true&Ajax_CallBackType=Mtime.Library.Services&Ajax_CallBackMethod=GetMovieOverViewRating&Ajax_CrossDomain=1&Ajax_RequestUrl=http%3A%2F%2Fmovie.mtime.com%2F233465%2F&t=2018525956832821&Ajax_CallBackArgument0=233465"

d=difflib.Differ()
diff=d.compare(url1.splitlines(),url2.splitlines())
print '\n'.join(list(diff))