python - 为什么使用 difflib.ndiff 会得到杂散元素?

标签 python python-3.x difflib

最小工作示例:

In [3]: a = ('r1', 'r2', 'r11', 'r6', 'r1', 'r2', 'r7', 'r8')                                                                                           

In [4]: b = ('r1', 'r2', 'r1', 'r6', 'r1', 'r2', 'r7', 'r8')                                                                                            

In [5]: list(difflib.ndiff(a, b))                                                                                                                       
Out[5]: 
['  r1',
 '  r2',
 '- r11',
 '?   -\n',
 '+ r1',
 '  r6',
 '  r1',
 '  r2',
 '  r7',
 '  r8']

有人可以解释一下为什么输出列表中的第四个元素是换行符吗?我该怎么做才能不将该元素作为 ndiff 输出,而只获取列表的其余部分?

最佳答案

因为 ndiff 期望您传入的行以换行符结尾,如下所示:

a = ('r1\n', 'r2\n', 'r11\n', 'r6\n', 'r1\n', 'r2\n', 'r7\n', 'r8\n')
b = ('r1\n', 'r2\n', 'r1\n', 'r6\n', 'r1\n', 'r2\n', 'r7\n', 'r8\n')

difflib.Differ.compare 的文档中,这就是 .ndiff() 调用的 under the hood ,我们看到这个(强调我的):

compare(a, b)

Compare two sequences of lines, and generate the delta (a sequence of lines).

Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the readlines() method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the writelines() method of a file-like object.

您得到的输出是有意义的,以 ? 开头的行用于突出显示更改的内容。在本例中,它会在 r11 中的第二个 1 下绘制一个 -,以表明它已被删除。 difflib 期望您使用这样的输出

print(''.join(difflib.ndiff(a, b)))

所以它需要用换行符结束它添加的任何行。

您可以使用列表理解将换行符添加到原始值

a = [line + "\n" for line in a]
b = [line + "\n" for line in b]

关于python - 为什么使用 difflib.ndiff 会得到杂散元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54960450/

相关文章:

python - WARN : Tried to pass invalid video frame, 标记为损坏的 : Your frame has data type int64, 但我们需要 uint8

python - 小时、日期、天数计算

Python 图形库

python - 使用 difflib.get_close_matches() 忽略大小写

python - 在 python 中生成和应用差异

python - 如何使用 ssl 加密 cx_oracle 连接?

python - 如何对列表中的某些 float 求和

python-3.x - 在 Python 中的置换树上实现深度优先搜索

python - 如何测试\symbol(反斜杠)是否在字符串中?

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