I want to search and match a particular word in a text file.
with open('wordlist.txt', 'r') as searchfile:
for line in searchfile:
if word in line:
print line
This code returns even the words that contain substrings of the target word. For example if the word is "there" then the search returns "there", "therefore", "thereby", etc.
I want the code to return only the lines which contain "there". Period.
You can always use regex, something along the lines of:
\sthere\s
- any space followed by 'there' followed by any spacere.I
- means case insensitivere.M
- doesn't really matter in this case (since lines only have 1 \n)The
re.search
function scans the stringline
and returns true if it finds the regular expression defined in the first parameter, ignoring case withre.I
. The^
character means 'beginning of the line' while the$
character means 'end of the line'. Therefore, the search function will only return true if it matches there preceded by the beginning of the line, and followed by the end of the line, aka isolated on its own.split the line into tokens:
if word in line.split():
Look up the re module (regular expressions). re.search with the regex ' there ' is what you want.
You ought to use a regular expression. The regular expression howto from the Python docs might be a good place to start.