pythonwordsearch

python search file for a list of words


First I started trying to search file for one single word with this code:

import re

shakes = open("tt.txt", "r")

for line in shakes:
    if re.match("(.*)(H|h)appy(.*)", line):
        print line,

but what if I need to check for multiple words? I was thinking that maybe something like a for loop can work, searching the file each time for a different word in the list.

Do you think this can be convenient?


Solution

  • Just join the word_list with | as delimiter. (?i) case-insensitive modifier helps to do a case-insensitive match.

    for line in shakes:
        if re.search(r"(?i)"+'|'.join(word_lst), line):
            print line,
    

    Example:

    >>> f = ['hello','foo','bar']
    >>> s = '''hello
    hai
    Foo
    Bar'''.splitlines()
    >>> for line in s:
            if re.search(r"(?i)"+'|'.join(f), line):
                print(line)
    
    
    hello
    Foo
    Bar
    

    Without regex:

    >>> f = ['hello','foo','bar']
    >>> s = '''hello
    hai
    Foo
    Bar'''.splitlines()
    >>> for line in s:
            if any(i.lower() in line.lower() for i in f):
                print(line)
    
    
    hello
    Foo
    Bar