newbie str to int

Wojtek Walczak gminick at hacker.pl
Tue Dec 4 11:06:18 EST 2001


Dnia Wed, 03 Oct 2001 23:11:00 GMT, Andrei Kulakov napisał(a):
>On Tue, 2 Oct 2001 12:22:29 +0800, James <no at email.com> wrote:
>> I am switching from Perl to Python, and would
>> like to convert string '123abc' to integer 123.
>> foo = int('123abc') # error
>> foo = string.atoi('123abc') #error
>> foo = eval('123abc') # error
>import string
>lst = ""
>for char in "123abc":
>    if char in string.digits:
>        lst += char
>    else:
>        break
>num = int(lst)
or sth like that:

a = '123abc456gfh'
b = []

for i in a:
   if i.isdigit():
   b = b +i
print b # 123456

HTH.

-- 
[ Wojtek gminick Walczak ][ http://hacker.pl/gminick/ ]
[ gminick (at) hacker.pl ][ gminick (at) klub.chip.pl ]



More information about the Python-list mailing list