string.split bug?

Discussion in 'Python' started by MetalOne, Feb 25, 2004.

  1. MetalOne

    MetalOne Guest

    string.split("") ==> []
    string.split("",",") ==> ['']

    I did not expect these to have different outputs.

    I have a string with comma delimited numbers.
    There can be zero or more numbers in the string
    s = "0x41, 0x42"

    I wanted to do
    numbers = map(lambda x: int(x,16), string.split(s,","))

    However, when there are no numbers, this generates an error.
     
    MetalOne, Feb 25, 2004
    #1
    1. Advertising

  2. MetalOne

    Peter Otten Guest

    MetalOne wrote:

    > string.split("") ==> []
    > string.split("",",") ==> ['']
    >
    > I did not expect these to have different outputs.
    >
    > I have a string with comma delimited numbers.
    > There can be zero or more numbers in the string
    > s = "0x41, 0x42"
    >
    > I wanted to do
    > numbers = map(lambda x: int(x,16), string.split(s,","))
    >
    > However, when there are no numbers, this generates an error.


    It's not a bug, it's a feature. The same question was asked on python-dev
    recently, and it turned out that str.split() and str.split(separator) are
    intended to work differently.

    A slightly modernized variant of your example could then be:

    >>> def numbers(s, sep=None):

    .... return [int(x, 16) for x in s.split(sep) if x]
    ....
    >>> numbers("aa bb cc\n")

    [170, 187, 204]
    >>> numbers("aa,bb,cc", ",")

    [170, 187, 204]
    >>> numbers("", ",")

    []

    Peter
     
    Peter Otten, Feb 25, 2004
    #2
    1. Advertising

  3. MetalOne wrote:

    > string.split("") ==> []
    > string.split("",",") ==> ['']
    >
    > I did not expect these to have different outputs.


    S.split(None) is a special case which keys off of all whitespace, not
    just a single delimiter string. So when presented with an input string
    that contains nothing but whitespace, it strips everything and doesn't
    find any tokens at all.

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    \__/ Who, my friend, can scale Heaven?
    -- _The Epic of Gilgamesh_
     
    Erik Max Francis, Feb 25, 2004
    #3
    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:
    490
  2. Carlos Ribeiro
    Replies:
    11
    Views:
    735
    Alex Martelli
    Sep 17, 2004
  3. Simon Strandgaard

    [bug] String#split returns extra empty string

    Simon Strandgaard, May 31, 2004, in forum: Ruby
    Replies:
    8
    Views:
    370
    David A. Black
    Jun 1, 2004
  4. Sam Kong
    Replies:
    5
    Views:
    275
    Rick DeNatale
    Aug 12, 2006
  5. Stanley Xu
    Replies:
    2
    Views:
    705
    Stanley Xu
    Mar 23, 2011
Loading...

Share This Page