【发布时间】:2015-11-11 00:45:18
【问题描述】:
我有两个文件:
文件 1:
military troop deployment number need
文件 2:
foreign 1242
military 23020
firing 03848
troop 2939
number 0032
dog 1234
cat 12030
need w1212
我想从文件 1 中读取该行并打印这些单词以及它们在文件 2 中的行号。
我的输出应该是这样的:
military 2, troop 4, deployment <does not exist>, number 5, need 8
我试过代码:
words= 'military troop deployment number need'
sent = words.split()
print sent
with open("file2","r") as f1:
for line_num,line in enumerate(f1):
if any([word in line for word in sent]):
print line_num, line
这是打印这些单词所在的所有行。除此之外,它还打印诸如 pre-military、necessively 等字词。我只需要那些确切的字词和它们的行号。请帮忙
【问题讨论】:
标签: python file python-2.7 numbers line