check to see if value can be an integer instead of string

Gerard Flanagan grflanagan at yahoo.co.uk
Wed Jan 18 15:05:59 EST 2006


nephish at xit.net wrote:

> it isn't really that i will want to change it to an integer anyway. the
> script uses a table to reference a value to a key, if the key is a
> group of letters, that code tells the script to do something. if the
> value is a number, it means an equipment failure. The thing is, all the
> values come out as strings (they are read from a text file).
> so what you put first with the try/except looks like my best answer.
>
> thanks,
> shawn

Shawn

Are you aware of the string method 'isdigit()' ?

vars = ["KEY_ONE", "KEY_2", "0", "1", "24", "00100"]
for var in vars:
    if not var.isdigit():
        print "OPTION: %s" % var
    else:
        print "EQUIPMENT FAILURE: %s" % var

OPTION: KEY_ONE
OPTION: KEY_2
EQUIPMENT FAILURE: 0
EQUIPMENT FAILURE: 1
EQUIPMENT FAILURE: 24
EQUIPMENT FAILURE: 00100

( 'isdigit' != 'is_a_digit'  rather 'isdigit' ==
'is_a_string_of_digits' )

Gerard




More information about the Python-list mailing list