Avoiding "invalid literal for int()" exception
Peter Otten
__peter__ at web.de
Tue Dec 12 03:52:30 EST 2006
Marc 'BlackJack' Rintsch wrote:
> In <mailman.1415.1165879866.32031.python-list at python.org>, Gabriel
> Genellina wrote:
>
>> At Monday 11/12/2006 07:22, aine_canby at yahoo.com wrote:
>>
>>>elif int(uniList[0]) in range(0,10):
>>
>> Either of these will work to avoid an unneeded conversion:
>>
>> elif uniList[0] in "0123456789":
>>
>> elif uniList[0] in string.digits:
>>
>> elif uniList[0].isdigit():
>
> The last does not work. Not only that it accepts numbers greater than 9
> because it checks if the whole string consists of digits, it also accepts
> u'²?' and other unicode digits.
By the way, if you require an implicit 0 <= int(s) < 10 check, none of the
above work with the newer Pythons:
>>> s = "23"
>>> s in "0123456789"
True
>>> s in set("0123456789")
False
Peter
More information about the Python-list
mailing list