On Mar 21, 1:42 pm, "PKKR" <[EMAIL PROTECTED]> wrote:
> I need a fast and efficient way to parse a combination string(digits +
> chars)
>
> ex: s = "12ABA" or "1ACD" or "123CSD" etc
>
> I want to parse the the above string such that i can grab only the
> first digits and ignore the rest of the chacters,
>
> so if i have s = "12ABA" , parser(s) should give me "12" or "1" or
> "123".
>
> I can think of a quick dirty way by checking each element in the
> string and do a 'str.isdigit()' and stop once its not a digit, but
> appreciate any eligent way.

Somehow you'll need to test each element in the string to know if it
is an integer or not. You could do the str.isdigit() method or you
could do something like this:

temp = ''
for i in yourString:
        try:
                int(i)
                temp += i
        except:
                pass


Maybe someone knows of a parser. However, that will likely be more
complex.

Mike

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to