Home > database >  only keep certain words in a string python
only keep certain words in a string python

Time:03-22

I was wondering if there was a way to strip away text that isn't the text I needed. I need to remove all text from a screenshot that isn't "loud noises". I've searched all over, but I haven't found an answer. My code:

if "loud noises" in text:
    print(text)
else:
    pass

I need a way to only display the text "loud noises", and nothing else. (it's a long story)

CodePudding user response:

what you could do is:

def getWord(idx,word,get):
    try:
        total = 0
        for i in range(len(get)):
            if word[idx   i] == get[i]:
                total  = 1
        if total == len(get):
            return True
        else:
            return False
    except IndexError:
        # out of range
        return
text = "I make loud noises"
newtext = ""
for idx in range(len(text)):
    if getWord(idx,text,"loud noises"):
        newtext  = "loud noises"

print(newtext)

It accomplishes what you're thinking of, and could be used as mehtod.

  • Related