using split for a string : error
Chris Angelico
rosuav at gmail.com
Thu Jan 24 20:07:35 EST 2013
On Fri, Jan 25, 2013 at 11:20 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> 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?
123, 0, and 1. That's standard atoi semantics.
> 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.
Sure. But I'm not trying to cater to people who get it wrong; that's a
job for a DWIM.
> 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.
And yes, I'm fully aware that I can roll my own. Here's a shorter
version (ASCII digits only, feel free to expand to Unicode), not
necessarily better:
def atoi(s):
return int("0"+s[:-len(s.lstrip("0123456789"))])
It just seems silly that this should have to be done separately, when
it's really just a tweak to the usual string-to-int conversion: when
you come to a non-digit, take one of three options (throw error, skip,
or terminate).
Anyway, not a big deal.
ChrisA
More information about the Python-list
mailing list