获取文件中存在的单词行数

时间:2015-11-11 00:45:18

标签: python file python-2.7 numbers line

我有两个文件:

文件1:

military  troop deployment number need  

文件2:

foreign 1242
military 23020
firing  03848
troop 2939
number 0032
dog 1234
cat 12030
need w1212

我想从文件1中读取该行并打印这些单词以及它们在file2中的行号。

我的输出应该是:

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

这是打印这些单词所在的所有行。除此之外,它还打印像军事前,不必要的......等字样。我只需要那些确切的单词和它们的行号。请帮忙

1 个答案:

答案 0 :(得分:2)

你正在打印错误的东西。您想要打印单词而不是整行。此外,如果您使用any,则不知道哪个词匹配。

这是两种方法。第一个没有检测到空条目。

words= 'military  troop  deployment  number  need'
sent = words.split()

matched = []
with open("file2","r") as f1:
    for i, line in enumerate(f1):
        for word in sent:
            if word in line:
                matched.append('%s %d' % (word, i + 1))

print ', '.join(matched)

<强>输出:

military 2, troop 4, number 5, need 8

如果你想打印空条目。

words= 'military  troop  deployment  number  need'
sent = words.split()

linenos = {}

with open("file2","r") as f1:
    for i, line in enumerate(f1):
        for word in sent:
            if word in line:
                linenos[word] = i + 1

matched2 = []
for word in sent:
    if word in linenos:
        matched2.append('%s %d' % (word, linenos[word]))
    else:
        matched2.append('%s <does not exist>' % word)
print ', '.join(matched2)

<强>输出:

military 2, troop 4, deployment <does not exist>, number 5, need 8

处理单词的多次出现并仅打印第一行。

words= 'military  troop  deployment  number  need'
sent = words.split()
linenos = {}

with open("file2", "r") as f1:
    for i, line in enumerate(f1):
        for word in sent:
            if word in line:
                if word in linenos:
                    linenos[word].append(i + 1)
                else:
                    linenos[word] = [i + 1]

matched2 = []
for word in sent:
    if word in linenos:
        matched2.append('%s %r' % (word, linenos[word][0]))
    else:
        matched2.append('%s <does not exist>' % word)

print ', '.join(matched2)

与前一个示例相同的输出。