how can i check if value is integer or string? Err in conversion

Asked by Adrian on 2018-03-25

Hi there,

after correct OCR from images, removing any dots commas and letters if recognised by mistake, than convert string to int and tried to compare with variable - digits. Im looking to use function to show wheter value is string or int or float or whatever it is to check if my conversions was successful or not. This is my simple code.

---
# set variables to compare
vpmin = int("40000")
vpmax = int("349999")

if exists("1521886809839.png"):
        vpvalue=find("1521886809839.png").text()
# replace any unnessery trash as a string
        vpvalue = vpvalue.replace("X", "") # 1
        vpvalue = vpvalue.replace(".", "") # 2
        vpvalue = vpvalue.replace(",", "") # i know i could do it with regex or something ;)
        converted_vpvalue = int(vpvalue)

# compare values
if converted_vpvalue >= vpmin and converted_vpvalue <= vpmax:
        do some actin...
---

Got error "TypeError: cannot concatenate 'str' and 'int' objects" but thought that int() converted all correctly, and dont know what is not converted - variables at start or converted_vpvalue

when debuggin variables before some actions i got
clean OCR'ed value "X259.240"
after sting trash replace "259240"
and when converted was same as replaced value

OCR - im getting text via find().text() coz i think that in my situation is faster than region().text() and got less script termination... but not certain and im using if exists to get OCR becouse sometimes lasts more than 3 sec to render action with correct images - avoiding error in script, works great.

1 OCR gives me string coz value on img is with two crossed swords at the begining which is recognized as "X" ;)
2 removing X, dot or comma before conversion to int coz i think that if value is 350.200 its divide it to two diffrent numbers when tried to compare in if statement

Weird is that when variables vpmin and vpmax was set like vpmax="349999" w/o int() all was workin ok, probably script was comparing both values as string but was not accurate and sometimes didn't pass through if statement when matched (but rather was correct and all was workin)

tried few stuff from forum like https://answers.launchpad.net/sikuli/+question/123516 but nothin helped

im not a regular programmer or something but after few days spent in this forum lurking for my problems i had at the beginning of my scripting and reading sikuli doc i have no problems with creating automation of my stuff beyond this conversion and comparision two values.
Is something wrong with conversion or exists function to check wheter type the value is.. i know i could leave conversion and still check two strings like before, but you know... more precise means always better ;)

Thank You in advance,
Adrian

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2018-04-14
Last query:
2018-04-14
Last reply:
2018-03-25
Adrian (adek-) said : #1

Hi there,

after correct OCR from images, removing any dots commas and letters if recognised by mistake, than convert string to int and tried to compare with variable - digits. Got TypeError: cannot concatenate 'str' and 'int' objects. Im looking to use function to show wheter value is string or int or float or whatever it is to check if my conversions was successful or not. This is my simple code.

# set variables to compare
vpmin = int("40000")
vpmax = int("349999")

# OCR text via find().text() i think that in my situation is faster than region().text() but not certain

---
if exists("1521886809839.png"):
        vpvalue=find("1521886809839.png").text() # OCR gives me string coz value on img is with two crossed swords at the begining which is recognized as "X"
# replace any unnessery trash as a string
        vpvalue = vpvalue.replace("X", "") # 1
        vpvalue = vpvalue.replace(".", "") # 2
        vpvalue = vpvalue.replace(",", "") # i know i could do it with regex or something ;)
        converted_vpvalue = int(vpvalue)

# compare values
if converted_vpvalue >= vpmin and converted_vpvalue <= vpmax:
---

Got error "TypeError: cannot concatenate 'str' and 'int' objects" but thought that int() converted all correctly, and dont know what is not converted - variables at start or converted_vpvalue

Im using if exists to get OCR becouse sometimes lasts more than 3 sec to render action with correct images - avoiding error in script, works great
1 like i said crossed swords are recognised as "X" ;)
2 removing dot or comma before conversion to int coz i think that if value is 350.200 its divide it to two diffrent numbers when tried to compare in if statement

Weird is that when variables vpmin and vpmax was set like vpmax="349999" w/o int() all was workin ok, probably script was comparing both values as string but was not accurate and sometimes didn't pass through if statement when matched (but rather was correct and all was workin)

when debuggin variables before some actions i got
clean OCR'ed value "X259.240"
after replace "259240"
and when converted was same as replaced value

tried few stuff from forum like https://answers.launchpad.net/sikuli/+question/123516 but nothin helped

im not a regular programmer or something but after few days spent in this forum lurking for my problems i had at the beginning of my scripting and reading sikuli doc i have no problems with creating automation of my stuff beyond this conversion and comparision two values.
Is something wrong with conversion or exists function to check wheter type the value is.. i know i could leave conversion and still check two strings like before, but you know... more precise means always better ;)

Thank You in advance,
Adrian

RaiMan (raimund-hocke) said : #2

this is not needed this way
vpmin = int("40000")
vpmax = int("349999")

use
vpmin = 40000
vpmax = 349999

this is ok:
        vpvalue = vpvalue.replace("X", "") # 1
        vpvalue = vpvalue.replace(".", "") # 2
        vpvalue = vpvalue.replace(",", "") # i know i could do it with regex or something ;)
        converted_vpvalue = int(vpvalue)

in the end converted_vpvalue contains a number (integer).

This is also ok:
if converted_vpvalue >= vpmin and converted_vpvalue <= vpmax:

... so I cannot see in your snippet any reason for
"cannot concatenate 'str' and 'int' objects"

so the problem must be somewhere else (the error should show a line number)

If you have the error location the solution is:
targetString = someString + str(someNumber)

Best RaiMan (raimund-hocke) said : #3

... or if it is for printing use the % format operator:
print "the number is &d" % (someNumber)

Adrian (adek-) said : #4

Thanks RaiMan, that solved my question.