I want to take word input from the user and find those words from a text file where I have many other words. Some words are repeating and I want them also in the same manner. I am using 're' in Anaconda Spyder to run my code. I can do it successfully and get the output text file with searched words. But for this, I am defining the words inside my code. I don't understand how can I take an input word for this operation. The input is coming from a GUI checkbox selection. So I need to save the input words in a string and use that. I need to use '|' operator. Here is my code
import re
from pathlib import Path
#words are=== mango, cat, dog, tiger, three, hat, hot, sweden, kth
wordset = Path('words.txt').read_text()
#wordset = wordset.replace('\n', '')
# lst = ['kth','cat','tiger']
#####here in the words_record section, it should get the key-words from the user's input or another code's output result. so basically the words are going to a string. but I am unable to use string value here. The user will give both single word input and multiple word input.
words_record = 'kth|cat|tiger'
#XY = re.findall(words_record, wordset)
XY = re.findall(words_record, wordset, flags=re.IGNORECASE)
# print(XY)
with open('filtered_list.txt', 'w') as filehandle: ###save output in textfile
for listitem in XY:
filehandle.write('%s\n' % listitem)