def line_number(word, fname):
with open(fname) as f:
number_list = ""
for i, line in enumerate(f, 1):
if word in line:
number_list += (str(i)+", ")
return number_list[:-2]
The function above is suppose to find the line number on a txt file which a matching string occurs. However, for example, if we are searching the string "yes", and we have string "yes" on the 20th line and "eyes" on the 51st line, the function will return line 20 and line 51 because line 51 contains a substring "yes" in "eyes", how can I fix this bug?
Okay, I have solved the problem by changing if word in line:
to if word in re.split('(\W+)', line):
By doing so I split the line into words and punctuation to find the exact match.
But I noticed anohter problem. For example, on line 159
, there is a sentence of "you you you." the word you
appear 3 times, the function only counts you
appear once in line 159
, and the program prints out:
you 159
but I want the function to count it 3 times and prints out:
you 159, 159, 159
Is there any way to do it?