Re: using split for a string : error

Discussion in 'Python' started by Oscar Benjamin, Jan 25, 2013.

  1. On 24 January 2013 11:35, 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.
    >


    I'm interested to know what the situations are where you want the
    behaviour of atoi().

    Personally, I consider the int() function too permissive because of
    its behaviour in truncating non-integer numeric types. But then that's
    because I'm always paranoid that the values of my precious numbers are
    being changed without my knowledge. From my vantage point I really
    can't see why the ambiguous behaviour of atoi() would actually be
    desired by anyone (unless they were stuck using a language that made
    string manipulation generally a bit awkward).


    Oscar
    Oscar Benjamin, Jan 25, 2013
    #1
    1. Advertising

  2. Oscar Benjamin

    Neil Cerutti Guest

    On 2013-01-25, Oscar Benjamin <> wrote:
    > On 24 January 2013 11:35, 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.

    >
    > I'm interested to know what the situations are where you want
    > the behaviour of atoi().


    Right. atoi is no good even in C. You get much better control
    using the sprintf family. int would need to return a tuple of the
    number it found plus the number of characters consumed to be more
    useful for parsing.

    >>> intparse("123abc")

    (123, 3)

    But that would make it might inconvenient for general use.

    --
    Neil Cerutti
    Neil Cerutti, Jan 25, 2013
    #2
    1. Advertising

  3. Oscar Benjamin

    Hans Mulder Guest

    On 25/01/13 15:04:02, Neil Cerutti wrote:
    > On 2013-01-25, Oscar Benjamin <> wrote:
    >> On 24 January 2013 11:35, 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.

    >>
    >> I'm interested to know what the situations are where you want
    >> the behaviour of atoi().

    >
    > Right. atoi is no good even in C. You get much better control
    > using the sprintf family.


    I think you meant sscanf.

    It's true that sscanf gives you more control. That being said,
    sometimes the one option atoi gives you, just happens to be what
    you need.

    > int would need to return a tuple of the
    > number it found plus the number of characters consumed to be more
    > useful for parsing.
    >
    >>>> intparse("123abc")

    > (123, 3)
    >
    > But that would make it might inconvenient for general use.


    If the new function is nameed intparse, and the existing int
    function remains available, then most use cases would be served
    by int, and intparse would be available as a building block for
    other use cases. For example atoi could be defined as:

    def atoi(s): return intparse(s)[0]

    intparse("xyz") should return (0, 0), and leave it to the caller
    to decide whether a ValueError shoud be raised.


    -- HansM
    Hans Mulder, Jan 25, 2013
    #3
  4. Don't forget to look at csv reader.

    http://docs.python.org/2/library/csv.html


    On Fri, Jan 25, 2013 at 9:31 AM, Hans Mulder <> wrote:

    > On 25/01/13 15:04:02, Neil Cerutti wrote:
    > > On 2013-01-25, Oscar Benjamin <> wrote:
    > >> On 24 January 2013 11:35, 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.
    > >>
    > >> I'm interested to know what the situations are where you want
    > >> the behaviour of atoi().

    > >
    > > Right. atoi is no good even in C. You get much better control
    > > using the sprintf family.

    >
    > I think you meant sscanf.
    >
    > It's true that sscanf gives you more control. That being said,
    > sometimes the one option atoi gives you, just happens to be what
    > you need.
    >
    > > int would need to return a tuple of the
    > > number it found plus the number of characters consumed to be more
    > > useful for parsing.
    > >
    > >>>> intparse("123abc")

    > > (123, 3)
    > >
    > > But that would make it might inconvenient for general use.

    >
    > If the new function is nameed intparse, and the existing int
    > function remains available, then most use cases would be served
    > by int, and intparse would be available as a building block for
    > other use cases. For example atoi could be defined as:
    >
    > def atoi(s): return intparse(s)[0]
    >
    > intparse("xyz") should return (0, 0), and leave it to the caller
    > to decide whether a ValueError shoud be raised.
    >
    >
    > -- HansM
    >
    >
    >
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Joel Goldstick
    http://joelgoldstick.com
    Joel Goldstick, Jan 25, 2013
    #4
  5. Oscar Benjamin

    Neil Cerutti Guest

    On 2013-01-25, Hans Mulder <> wrote:
    >> Right. atoi is no good even in C. You get much better control
    >> using the sprintf family.

    >
    > I think you meant sscanf.


    Yes, thanks for knocking that huge chunk of rust off of me. ;)

    --
    Neil Cerutti
    Neil Cerutti, Jan 25, 2013
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    453
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    688
    Alex Martelli
    Sep 17, 2004
  3. trans.  (T. Onoma)

    split on '' (and another for split -1)

    trans. (T. Onoma), Dec 27, 2004, in forum: Ruby
    Replies:
    10
    Views:
    203
    Florian Gross
    Dec 28, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    229
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    592
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page