4

我正在使用 Windows 7 中的 python 3.4。尝试比较两个文本文件,我想使用 difflib 报告它们之间的差异。以下是代码 m 使用:

import difflib
from difflib_data import *

with open("s1.txt") as f, open("s2.txt") as g:
    flines = f.readlines()
    glines = g.readlines()

d = difflib.Differ()
diff = d.compare(flines, glines)
print("\n".join(diff))

Traceback: from difflib_data import * ImportError: No module named 'difflib_data'

如何删除此错误....谢谢

4

1 回答 1

2

下面的帖子来看,它似乎是 PyMOTW 教程提供的示例数据。

我假设作者希望您将测试数据源复制并粘贴到工作目录中名为 difflib_data.py 的新文件中。

将以下行复制到 difflib_data.py

text1 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integereu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitortellus. Aliquam venenatis. Donec facilisis pharetra tortor. In necmauris eget magna consequat convallis. Nam sed sem vitae odiopellentesque interdum. Sed consequat viverra nisl. Suspendisse arcumetus, blandit quis, rhoncus ac, pharetra eget, velit. Maurisurna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl portaadipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristiqueenim. Donec quis lectus a justo imperdiet tempus."""

text1_lines = text1.splitlines()

text2 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integereu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitortellus. Aliquam venenatis. Donec facilisis pharetra tortor. In necmauris eget magna consequat convallis. Nam sed sem vitae odiopellentesque interdum. Sed consequat viverra nisl. Suspendisse arcumetus, blandit quis, rhoncus ac, pharetra eget, velit. Maurisurna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl portaadipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristiqueenim. Donec quis lectus a justo imperdiet tempus."""

text2_lines = text2.splitlines()
于 2014-09-17T06:52:42.010 回答