首页 > 代码库 > 字符串差异对比--结果生成HTML
字符串差异对比--结果生成HTML
#!/usr/bin/env python
import difflib
txt1 = """
IPython 0.13 -- An enhanced Interactive Python.
%quickref -> Quick reference.
help -> Python‘s own help system.
object? -> Details about ‘object‘, use ‘object??‘ for extra details.
"""
txt2 = """
? -> Introduction and overview of IPython‘s features.
%quickref -> Quick reference.
help -> Python‘s own help system. Thx!
object? -> Details about ‘Object‘, use ‘object??‘ for extra details.
"""
txt1_lines = txt1.splitlines()
txt2_lines = txt2.splitlines()
differ = difflib.HtmlDiff() # 实例化difflib的HtmlDiff
diff = differ.make_file(txt1_lines, txt2_lines) #调用make_file方法
print diff
将输出重定向到html文件中,结果如下:
本文出自 “技术传承与分享” 博客,请务必保留此出处http://pythonfan.blog.51cto.com/9764080/1599362
字符串差异对比--结果生成HTML