1

昨天我一直在实现一个小的 Python 脚本,它检查两个文件之间的差异(使用 difflib),如果有则打印结果,否则以代码 0 退出。

精确的方法difflib.unified_diff()是在找到的差异上返回一个生成器。如何测试此生成器以查看是否需要打印?我尝试使用len(),sum()查看此生成器的大小,但无法打印。

很抱歉问了这么一个愚蠢的问题,但我真的不明白在这个话题上有什么好的做法。

到目前为止,这就是我正在做的

import difflib
import sys

fromlines = open("A.csv").readlines()
tolines = open("B.csv").readlines()
diff = difflib.unified_diff(fromlines, tolines, n=0)
if (len(list(diff))):
    print("Differences found!")
    # Recomputing the generator again: how stupid is that!
    diff = difflib.unified_diff(fromlines, tolines, n=0)
    sys.stdout.writelines(diff)
else:
    print("OK!")
4

3 回答 3

1

您已经将生成器转换为列表,因此无需重新构建它。

diff = list(difflib.unified_diff(fromlines, tolines, n=0))
if diff:
    ...
    sys.stdout.writelines(diff)
else:
    ...

如果您不想使用简单的标志,您甚至不需要将生成器转换为列表:

diff = difflib.unified_diff(fromlines, tolines, n=0)
f = False
for line in diff:
    if not f:
        print("Differences found!")
        f = True
    sys.stdout.write(line)

if not f:
    print("OK!")
于 2013-07-26T07:57:09.443 回答
0

我认为你不能,正确的方法可能是生成所有数据,直到你提出 StopIteration 然后得到你生成的长度。

有什么问题:

import difflib
import sys

fromlines = open("A.csv").readlines()
tolines = open("B.csv").readlines()
diff = difflib.unified_diff(fromlines, tolines, n=0)
difflines = list(diff)
if len(difflines) :
    sys.stdout.writelines(difflines)
else:
    print("OK!")
于 2013-07-26T07:59:55.923 回答
0

您可以将生成器转换为列表。

diff = list(difflib.unified_diff(fromlines, tolines, n=0))
于 2013-07-26T07:56:57.417 回答