[Tutor] Searching through text with multiple answers
Alex Kleider
akleider at sonic.net
Sun Nov 17 12:21:30 EST 2019
On 2019-11-17 08:42, samhar zghaier wrote:
> Hello
> I'm trying to make a search function through a small text file
> i created a function to split my text and put into a list
>
> my goal right now is to create a search function that shows tha the
> results of my search and confirms its existence in the text file.
> however i
> do not know how to do it if a few of my elements share a name
Can you be more specific as to for what it is that you are searching?
Some examples would be most helpful.
Then you'd be in a better position to begin writing a function to
accomplish same.
> def split_text():
> file_name = "text.txt"
> list_of_everything = []
>
> with open(file_name) as file:
> for line in file:
> list_of_everything.append(line.strip())
What follows might be another way of traversing your file:
def my_traverse(f_name):
ret = []
with open(f_name, 'r') as f_obj:
for line in f_obj:
function_yet_to_be_written(line, ret) #look for something in line
and if found, append to ret
return ret
More information about the Tutor
mailing list