0

I am new to python and am trying to create a function in python that finds the lines where the word occurs in a text file and prints the line numbers. The function takes the text file name and list of words as input. I don't know where to start.

Example

index("notes.txt",["isotope","proton","electron","neutron"])

isotope 1
proton 3
electron 2
neutron 5

This is some random code that I made with text; so, I don't know if it can help me or not.

def index():
    infile=open("test.txt", "r")
    content=infile.read()
    print(content)
    infile.close()

The goal is to be able to find the words in the text file like a person would find a word in the index of a book.

KMaelstrom
  • 47
  • 1
  • 1
  • 6

3 Answers3

5

try like this:

def word_find(line,words):
    return list(set(line.strip().split()) & set(words))

def main(file,words):
    with open('file') as f:
        for i,x in enumerate(f, start=1):
            common = word_find(x,words)
            if common:
                print i, "".join(common)

if __name__ == '__main__':
    main('file', words)
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • looks like people just going through downvoting all the answers since this is an admittedly poorly-formed question. I'd originally gone looking for an exact dupe but I can't find anything that's asking to match a LIST of words. – Adam Smith Jan 13 '15 at 02:43
  • I'm sorry for not wording my question better. I'm taking a python class at community college, and this is the homework. I've been working on it for a few days, but I cannot figure it out. The assignment says that the function takes the text file name and a list of words as input. I have not been downvoting anybody. Anyways, thanks for your answers, everyone! – KMaelstrom Jan 13 '15 at 03:02
3
words = ['isotope', 'proton', 'electron', 'neutron']

def line_numbers(file_path, word_list):

    with open(file_path, 'r') as f:
        results = {word:[] for word in word_list}
        for num, line in enumerate(f, start=1):
            for word in word_list:
                if word in line:
                    results[word].append(num)
    return results

This will return a dictionary that has all the occurrences of the given word (case-sensitive).

DEMO

>>> words = ['isotope', 'proton', 'electron', 'neutron']
>>> result = line_numbers(file_path, words)
>>> for word, lines in result.items():
        print(word, ": ", ', '.join(lines))
# in your example, this would output:
isotope 1
proton 3
electron 2
neutron 5
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Adam Smith's answer broke in Python3.7. I needed to map to a string as follows:

for word, lines in result.items():
    print(word, ": ", ', '.join(map(str,lines)))
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574