I was trying to make a parser in Python which goes through multiple files, searches for given words, and returns all the lines that contain that string.
The method I am using is making the same line from the document containing a searched word print multiple times, if that line contains multiple words that user is trying to search.
The search method I am using currently is :
for line in range(0,length_of_documents):
for word in range(0,words):
if words[word] in document[line]:
print(document[line])
To overcome this, I need to write something like :
for line in range(0,length_of_documents):
for word in range(0,words):
if words[0] in document[line] or if words[1] in document[line] or if words[2] in document[line]:
print(document[line])
But I don't know how many words the user can enter for the search string. What is a possible solution for this?
I have used eval() function which gets passed in the string dynamically generated 'if words[0] in document[line] or if words[1] in document[line] or........' during runtime, but that does not work. I get syntax error at 'if'.
Aucun commentaire:
Enregistrer un commentaire