gpt4 book ai didi

python - 如何使用 difflib.ndiff 忽略行?

转载 作者:行者123 更新时间:2023-11-28 21:30:37 27 4
gpt4 key购买 nike

根据文档,您可以提供一个 linejunk 函数来忽略 certian 行。但是,我无法让它工作。以下是一些供讨论的示例代码:

from re import search
from difflib import ndiff
t1 = 'one 1\ntwo 2\nthree 3'
t2 = 'one 1\ntwo 29\nthree 3'
diff = ndiff(t1.splitlines(), t2.splitlines(), lambda x: search('2', x))

我的意图是忽略第二行,diff 将是一个不显示任何差异的生成器。

感谢您的帮助。

最佳答案

我最近遇到了同样的问题。

这是我发现的:

比照。 http://bugs.python.org/issue14332

The main intent of the *junk parameters is to speed up matching to find differences, not to mask differences.

比较 http://hg.python.org/cpython/rev/0a69b1e8b7fe/

该补丁更好地解释了 difflib 文档中的“垃圾”和“忽略”概念

These junk-filtering functions speed up matching to find differences and do not cause any differing lines or characters to be ignored.

关于python - 如何使用 difflib.ndiff 忽略行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2030551/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com