字符串差异对比--结果生成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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。