Search code examples
pythonfor-loopany

How to check if a value matches a txt file


Im currently trying to solve a solution where I have a value and a text file (.txt) where I want to check if the value in the code is somewhere inside the text file.

What I have done currently is that I have a text file that looks like:

999486
1117978
990583
1128062
1120618

and a code that looks like:

def filter():

    item_name = '1128062'

    keyword = [line.rstrip('\n') for line in open('keywords.txt')]

    has_good = False

    sentences = [item_name]

    def check_all(sentence, ws):
        return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)

    for sentence in sentences:
        if any(check_all(sentence, word) for word in keyword):
            has_good = True
            break

    if not has_good or keyword == "":
        print("Removed the keyword - " + str(item_name))
        sys.exit()

What the script does is:

that it has a item_name that has a value. Opens up keywordwhere all the keyword are stored

With the check_all function and for sentence in sentences: my idea was to check if the keyword is matched in the txt file. And if it is then we just continue the program and if its not then we print out Removed the keyword and sys.exit the program.

However when I am trying to run this program now I am getting an error saying

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:/Users/test.py.py", line 324, in filter
    if any(check_all(sentence, word) for word in keyword):
  File "C:/Users/test.py.py", line 324, in <genexpr>
    if any(check_all(sentence, word) for word in keyword):
  File "C:/Users/test.py.py", line 321, in check_all
    return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
  File "C:/Users/test.py.py", line 321, in <genexpr>
    return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python36\lib\re.py", line 182, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or bytes-like object

I realized it must be something issue regarding

def check_all(sentence, ws):
    return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)

and that's where I have the issue and asking you guys how I can be able to check if the keyword matches in a .txt file and if it doesn't match then we print out Removed the keyword and sys.exit the program, if it matches then we don't do anything.


Solution

  • There is no need for the re module here, as it appears we are just searching for a string match.

    import sys
    
    KEYWORDS_PATH = 'keyword.txt'
    KEYWORDS = open(KEYWORDS_PATH).read().splitlines()
    
    sentences = ['999487']
    
    for sentence in sentences:
        if sentence in KEYWORDS:
            print('Removed the keyword - %s' % sentence)
            sys.exit()