python的包很丰富, difflib是用来做文本比较的,下面是一个使用例子:
import difflib
lines1 = '''
dog
cat
bird
buffalo
gophers
hound
horse
'''.strip().splitlines()
lines2 = '''
cat
dog
bird
buffalo
gopher
horse
mouse
'''.strip().splitlines()
# Changes:
# swapped positions of cat and dog
# changed gophers to gopher
# removed hound
# added mouse
for line in difflib.unified_diff(lines1, lines2, fromfile='file1', tofile='file2', lineterm=''):
print line
输出内容如下:
Outputs the following: --- file1 +++ file2 @@ -1,7 +1,7 @@ +cat dog -cat bird buffalo -gophers -hound +gopher horse +mouse
这个类库也可以用来比较两个文本文件。
