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.
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)
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'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)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.