String Comparison Does Not Work in Python

String Comparison Does Not Work in Python

I'm writing a script that work with tesseract-ocr. I get text from screen and then I need to compare it with a string. The problem is that the comparison fails even if I'm sure that the strings are the same.

How can I made my code works?

Here my code:

import pyscreenshot as pss
import time
from pytesser import image_to_string

buy=str("VENDI")
buyNow=str("VENDI ADESSO")
if __name__ == '__main__':
    while 1:
        c=0

        time.sleep(2)
        image=pss.grab(bbox=(1104,422,(1104+206),(422+30)))
        text = str(image_to_string(im))
        print text
        if text==buy or text==buyNow:
            print 'ok'

For example as input:

And as output I get:

VENDI ADESSO

Which is the same string I need to compare, but during the execution I don't get ok on the console?

12

1 Answer

As it turns out, your string has new-lines (\n\n) at the end.

You can use

text = text.strip()

to remove any surrounding whitespace from your string.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.