Print Word & Line Number Where Word Occurs in File Python

penmas Source

I am trying to print the word and line number(s) where the word occurs in the file in Python. Currently I am getting the correct numbers for second word, but the first word I look up does not print the right line numbers. I must iterate through infile, use a dictionary to store the line numbers, remove new line chars, remove any punctuation & skip over blank lines when pulling the number. I need to add a value that is actually a list, so that I may add the line numbers to the list if the word is contained on multiple lines.

Adjusted code:

def index(f,wordf):

    infile = open(filename, 'r')
    dct = {}
    count = 0
    for line in infile:
        count += 1
        newLine = line.replace('\n', ' ')
        if newLine == ' ':
            continue
        for word in wordf:
            if word in split_line:
                if word in dct:
                    dct[word] += 1
                else:
                    dct[word] = 1

    for word in word_list:
        print('{:12} {},'.format(word,dct[word]))
    infile.close()       

Current Output:

>>> index('leaves.txt',['cedars','countenance'])
pines        [9469, 9835, 10848, 10883],
counter      [792, 2092, 2374],

Desired output:

>>> index2('f.txt',['pines','counter','venison'])
pines       [530, 9469, 9835, 10848, 10883]
counter     [792, 2092, 2374]
python-3.x

Answers

answered 2 years ago brw59 #1

There is some ambiguity for how your file is set up, but I think it understand. Try this:

import numpy as np # add this import
...

    for word in word_f:
        if word in split_line:

            np_array = np.array(split_line)
            item_index_list = np.where(np_array == word)

            dct[word] = item_index_list # note, you might want the 'index + 1' instead of the 'index'

for word in word_f:
    print('{:12} {},'.format(word,dct[word]))
...

btw, as far as I can tell, you're not using your 'increment' variable.

I think that'll work, let me know if it doesn't and I'll fix it

answered 2 years ago brw59 #2

per request, I made an additional answer (that I think works) without importing another library

def index2(f,word_f):

    infile = open(f, 'r')
    dct = {}
    # deleted line
    for line in infile:
        newLine = line.replace('\n', ' ')
        if newLine == ' ':
            continue
        # deleted line
        newLine2 = removePunctuation(newLine)
        split_line = newLine2.split()
        for word in word_f:
            count = 0 # you might want to start at 1 instead, if you're going for 'word number'
            # important note: you need to have 'word2', not 'word' here, and on the next line
            for word2 in split_line: # changed to looping through data
                if word2 == word:                    
                    if word2 in dct:
                        temp = dct[word]
                        temp.append(count)
                        dct[word] = temp
                    else:
                        temp = []
                        temp.append(count)
                        dct[word] = temp
                count += 1
    for word in word_f:
        print('{:12} {},'.format(word,dct[word]))
    infile.close()    

Do be aware, I don't think this code will handle if the words passed in are not in the file. I'm not positive on the file that you're grabbing from, so I can't be sure, but I think it'll seg fault if you pass in a word that doesn't exist in the file.

answered 2 years ago brw59 #3

Note: I took this code from my other post to see if it works, and it seems that it does

def index2():

    word_list = ["work", "many", "lots", "words"]
    infile = ["lots of words","many many work words","how come this picture lots work","poem poem more words that rhyme"]
    dct = {}
    # deleted line
    for line in infile:
        newLine = line.replace('\n', ' ') # shouldn't do anything, because I have no newlines
        if newLine == ' ':
            continue
        # deleted line
        newLine2 = newLine # ignoring punctuation
        split_line = newLine2.split()
        for word in word_list:
            count = 0 # you might want to start at 1 instead, if you're going for 'word number'
            # important note: you need to have 'word2', not 'word' here, and on the next line
            for word2 in split_line: # changed to looping through data
                if word2 == word:
                    if word2 in dct:
                        temp = dct[word]
                        temp.append(count)
                        dct[word] = temp
                    else:
                        temp = []
                        temp.append(count)
                        dct[word] = temp
                count += 1
    for word in word_list:
        print('{:12} {}'.format(word, ", ".join(map(str, dct[word])))) # edited output so it's comma separated list without a trailing comma


def main():
    index2()


if __name__ == "__main__":main()

and the output:

work         2, 5
many         0, 1
lots         0, 4
words        2, 3, 3

and the explanation:

infile = [
"lots of words",                        # lots at index 0, words at index 2
"many many work words",                 # many at index 0, many at index 1, work at index 2, words at index 3
"how come this picture lots work",      # lots at index 4, work at index 5
"poem poem more words that rhyme"       # words at index 3
]

when they get appended in that order, they get the correct word placement position

answered 2 years ago penmas #4

My biggest error was that I was not properly adding the line number to the counter. I completely used the wrong call, and did nothing to increment the line number as the word was found in the file. The proper format was dct[word] += [count] not dct[word] += 1

def index(filename,word_list):

    infile = open(filename, 'r')
    dct = {}
    count = 0
    for line in infile:
        count += 1
        newLine = line.replace('\n', ' ')
        if newLine == ' ':
            continue
        newLine2 = removePunctuation(newLine)
        split_line = newLine2.split()
        for word in word_list:
            if word in split_line:
                if word in dct:
                    dct[word] += [count]
                else:
                    dct[word] = [count]
    for word in word_list:
        print('{:12} {}'.format(word,dct[word]))
    infile.close()

comments powered by Disqus