using split for a string : error

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Jan 24 19:20:42 EST 2013


Chris Angelico wrote:

> It's usually fine to have int() complain about any non-numerics in the
> string, but I must confess, I do sometimes yearn for atoi() semantics:
> atoi("123asd") == 123, and atoi("qqq") == 0. I've not seen a 
> convenient Python function for doing that. Usually it involves
> manually getting the digits off the front. All I want is to suppress
> the error on finding a non-digit. Oh well.

It's easy enough to write your own. All you need do is decide what you 
mean by "suppress the error on finding a non-digit".

Should atoi("123xyz456") return 123 or 123456?

Should atoi("xyz123") return 0 or 123?

And here's a good one:

Should atoi("1OOl") return 1, 100, or 1001?

That last is a serious suggestion by the way. There are still many people
who do not distinguish between 1 and l or 0 and O.

Actually I lied. It's not that easy. Consider:

py> s = '໑໒໙'
py> int(s)
129


Actually I lied again. It's not that hard:


def atoi(s):
    from unicodedata import digit
    i = 0
    for c in s:
        i *= 10
        i += digit(c, 0)
    return i


Variations that stop on the first non-digit, instead of treating them as
zero, are not much more difficult.



-- 
Steven




More information about the Python-list mailing list