【发布时间】:2016-12-01 04:49:08
【问题描述】:
我有两个文件,我正在尝试在两个文件之间打印唯一的句子。为此,我在 python 中使用 difflib。
text ='Physics is one of the oldest academic disciplines. Perhaps the oldest through its inclusion of astronomy. Over the last two millennia. Physics was a part of natural philosophy along with chemistry.'
text1 ='Physics is one of the oldest academic disciplines. Physics was a part of natural philosophy along with chemistry. Quantum chemistry is a branch of chemistry.'
import difflib
differ = difflib.Differ()
diff = differ.compare(text,text1)
print '\n'.join(diff)
它没有给我想要的输出。它给了我这样的。
P
h
y
s
i
c
s
i
s
o
n
e
o
f
t
h
e
我想要的输出只是两个文件之间的唯一句子。
text = 也许是最古老的,因为它包含了天文学。超过 过去两千年。
text1 = 量子化学是化学的一个分支。
另外,似乎 difflib.Differ 是逐行而不是逐句进行的。请有任何建议。我怎么能这样做?
【问题讨论】:
标签: python python-2.7 python-3.x pattern-matching difflib