0

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)

1 Answers1

0

If I run this code

import re 
wordset = "mango, cat, dog, tiger, three, hat, hot, sweden, kth"
words_record = 'kth|cat|tiger'
XY = re.findall(words_record, wordset, flags=re.IGNORECASE)
print(XY) #=> ["cat", "tiger", "kth"]

it works perfectly, as expected.

To get the regex-string one could use

raw_user_input_string.replace(' ','|').replace("\n",'|')

I'm not sure if i understood you correctly, but I think you were asking for way to achieve single/multi-line user input.

Multi-line user input in Python can be achieved as follows:

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break
text = '\n'.join(lines)

Answer from ZdaR https://stackoverflow.com/a/30239138/8106583

The user just submits an empty line if done. The variable text then contains a single or multiline string.

If you want the keywords in this special format "kth|cat|tiger" then just use something like

text.replace("\n",'|').replace(' ','|')
David Buck
  • 3,752
  • 35
  • 31
  • 35
wuerfelfreak
  • 2,363
  • 1
  • 14
  • 29
  • my main aim is to take input from a checkbox gui where user will click on one or more options. now when I read that in a string, and trying to use 're.findall(words_record, wordset, flags=re.IGNORECASE)' it is not working. saying str | str error – Sruti Bhattacharjee Dec 29 '20 at 11:11
  • I just tried your solution but it is not working. can you put the whole thing? if the input is 'cat, dog' then output is supposed to be 'cat, cat, dog, dog, cat' like that. there are multiple words in my text file 'words.txt' – Sruti Bhattacharjee Dec 29 '20 at 11:16
  • How is your `words.txt` formatted? Is it also a comma separated list? Maybe add an example of `words.txt` to your question. – wuerfelfreak Dec 29 '20 at 11:20
  • its like one word per sentence and some words are repeating. like mango then cat then hat then again mango then dog then dog then cat....etc – Sruti Bhattacharjee Dec 29 '20 at 11:47
  • I added someting to my answer. – wuerfelfreak Dec 29 '20 at 12:25
  • it's working for a single word or when the user is type 'cat|dog|kth' not cat then next line dog then next line kth. the thing is I need to use the operator '|' for the desired output. now the input will come from a GUI checkbox selection. so I need to store them in a string and use that. I understand your solution but it is not what I am looking for exactly. – Sruti Bhattacharjee Dec 29 '20 at 14:42
  • I'm sorry, that I could not help you. Good luck though – wuerfelfreak Dec 29 '20 at 15:06
  • it's okay. your solution is also very helpful. thank you so much :) – Sruti Bhattacharjee Dec 29 '20 at 15:07