Ana_Sam
Ana_Sam

Reputation: 479

Get line numbers of words present in a file

I have two files:

File 1:

military  troop deployment number need  

File 2 :

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

I want to read the line from file 1 and print those words and the line number they are in file2.

My output should be like:

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

I tried the code:

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

This is printing all the lines in which these words are. In addition to that it is also printing words like pre-military, needlessly,.. etc. I just need those exact words and their line numbers. Please help

Upvotes: 2

Views: 249

Answers (1)

Paul Rooney
Paul Rooney

Reputation: 21609

You are printing the wrong thing. You want to print the word not the whole line. Also if you use any, you do not know which word matched.

Here are 2 approaches. The first does not detect empty entries.

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)

Output:

military 2, troop 4, number 5, need 8

If you want to print the empty entries as well.

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)

Output:

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

To handle multiple occurrences of a word and print only the first line.

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)

Output same as previous example.

Upvotes: 2

Related Questions