Skip to main content
 首页 » 编程设计

python之获取文件中存在的单词的行号

2024年10月24日11lautakyan007

我有两个文件:

文件 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 

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

请您参考如下方法:

你打印错了。你想打印这个词而不是整行。此外,如果您使用 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) 

输出与前面的示例相同。