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:

Input image sample

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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like