给定两个文本文件A,B,在B中不存在B中行的行号的简单方法是什么?我看到有difflib,但没有看到用于检索行号的接口
答案 0 :(得分:11)
difflib可以满足您的需求。假设:
<强> A.TXT 强>
this
is
a
bunch
of
lines
<强> b.txt 强>
this
is
a
different
bunch
of
other
lines
像这样的代码:
import difflib
fileA = open("a.txt", "rt").readlines()
fileB = open("b.txt", "rt").readlines()
d = difflib.Differ()
diffs = d.compare(fileA, fileB)
lineNum = 0
for line in diffs:
# split off the code
code = line[:2]
# if the line is in both files or just b, increment the line number.
if code in (" ", "+ "):
lineNum += 1
# if this line is only in b, print the line number and the text on the line
if code == "+ ":
print "%d: %s" % (lineNum, line[2:].strip())
输出如下:
bgporter@varese ~/temp:python diffy.py
4: different
7: other
您还需要查看difflib代码"? "
并查看您希望如何处理该代码。
(另外,在实际代码中,您希望使用上下文管理器来确保文件被关闭等等)
答案 1 :(得分:1)
穷人的解决方案:
with open('A.txt') as f:
linesA = f.readlines()
with open('B.txt') as f:
linesB = f.readlines()
print [k for k, v in enumerate(linesB) if not v in linesA]