使用 difflib.HtmlDiff 类 - 显示单个字符

发布于 2024-11-15 01:37:55 字数 651 浏览 6 评论 0原文

我正在使用 difflib.HtmlDiff 类,使用两组文本(来自网站的 HTML)调用该函数,但是当它创建表格时

html_diff = difflib.HtmlDiff()
print html_diff.make_table(previous_contents, fetch_url.page_contents)

,似乎只是逐个字符进行比较(每个表格行 1 个字符),最终得到一个 4.3MB 的 txt两套html文件,只有100k。

文档文件说,

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.

但情况似乎并非如此。

有什么建议吗?

I am using the difflib.HtmlDiff class, calling the function using two sets of text (HTML from websites), however when it makes the table

html_diff = difflib.HtmlDiff()
print html_diff.make_table(previous_contents, fetch_url.page_contents)

however that just seems to compare char by char (1 char per table row), and I end up with a 4.3MB txt file for two sets of html which are only 100k.

The doc file says,

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.

however that doesn't seem to be the case.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-11-22 01:37:55

您提供的是字符串,而不是字符串(行)列表。

假设 UNIX 或 Windows 行结束:

print html_diff.make_table(previous_contents.split('\n'),
                           fetch_url.page_contents.split('\n'))

You're supplying strings, not lists of strings (lines).

Assuming UNIX or Windows line ends:

print html_diff.make_table(previous_contents.split('\n'),
                           fetch_url.page_contents.split('\n'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文