使用difflib.HtmlDiff类 - 显示单个字符
我正在使用difflib.HtmlDiff
这个类,调用它的功能来比较两段文本(来自网站的HTML),但是当它生成表格时,
html_diff = difflib.HtmlDiff()
print html_diff.make_table(previous_contents, fetch_url.page_contents)
看起来只是逐个字符地比较(每行表格一个字符),结果我得到的文本文件有4.3MB,而这两段HTML加起来才100KB。
文档里说,
Compares fromlines and tolines (lists of strings) and returns a string which is a
complete HTML file containing a table showing line by line differences with
inter-line and intra-line changes highlighted.
但实际情况似乎并不是这样。
有什么建议吗?
1 个回答
8
你提供的是字符串,而不是字符串的列表(行)。
假设使用的是UNIX或Windows的换行符:
print html_diff.make_table(previous_contents.split('\n'),
fetch_url.page_contents.split('\n'))