擅长:python、mysql、java
<p>如果重复和顺序无关紧要,这很简单:</p>
<pre><code>first = set(open('firstFile').readlines())
second = set(open('secondFile').readlines())
diff = second - first
</code></pre>
<p>如果输出顺序很重要:</p>
<pre><code>first = open('firstfile').readlines()
second = open('secondFile').readlines()
diff = [line for line in second if line not in first]
</code></pre>
<p>如果输入顺序很重要,那么问题需要澄清。你知道吗</p>
<p>如果文件足够大,将其加载到内存是个坏主意,则可能必须执行以下操作:</p>
<pre><code>secondFile = open('secondFile')
diffFile = open('diffFile')
for secondLine in secondFile:
match = False
firstFile = open('firstFile')
for firstLine in firstFile:
if firstLine == secondLine:
match = True
break
firstfile.close()
if not match:
print >>diffFile, secondLine
secondFile.close()
</code></pre>