String split

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michele Petrazzo

    String split

    Hello ng,
    I don't understand why split (string split) doesn't work with the same
    method if I can't pass values or if I pass a whitespace value:
    [color=blue][color=green][color=darkred]
    >>> "".split()[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> "".split(" ")[/color][/color][/color]
    ['']

    But into the doc I see:
    """ If sep is not specified or is None, any whitespace string is a
    separator.
    """

    In this two cases, split would to return the _same_ result?

    Thanks,
    Michele
  • Peter Otten

    #2
    Re: String split

    Michele Petrazzo wrote:
    [color=blue]
    > Hello ng,
    > I don't understand why split (string split) doesn't work with the same
    > method if I can't pass values or if I pass a whitespace value:
    >[color=green][color=darkred]
    >>>> "".split()[/color][/color]
    > [][color=green][color=darkred]
    >>>> "".split(" ")[/color][/color]
    > ['']
    >
    > But into the doc I see:
    > """ If sep is not specified or is None, any whitespace string is a
    > separator.
    > """[/color]

    The documentation for Python 2.4 has a better explanation.

    """
    split([sep [,maxsplit]])

    Return a list of the words in the string, using sep as the delimiter string.
    If maxsplit is given, at most maxsplit splits are done. (thus, the list
    will have at most maxsplit+1 elements). If maxsplit is not specified, then
    there is no limit on the number of splits (all possible splits are made).
    Consecutive delimiters are not grouped together and are deemed to delimit
    empty strings (for example, "'1,,2'.split(' ,')"returns "['1', '', '2']").
    The sep argument may consist of multiple characters (for example, "'1, 2,
    3'.split(', ')" returns "['1', '2', '3']"). Splitting an empty string with
    a specified separator returns "['']".

    If sep is not specified or is None, a different splitting algorithm is
    applied. First, whitespace characters (spaces, tabs, newlines, returns, and
    formfeeds) are stripped from both ends. Then, words are separated by
    arbitrary length strings of whitespace characters. Consecutive whitespace
    delimiters are treated as a single delimiter ("'1 2 3'.split()" returns
    "['1', '2', '3']"). Splitting an empty string or a string consisting of
    just whitespace returns an empty list.
    """
    Quoted after http://docs.python.org/lib/string-methods.html#l2h-202.

    Peter

    Comment

    • Fredrik Lundh

      #3
      Re: String split

      Michele Petrazzo wrote:
      [color=blue]
      > I don't understand why split (string split) doesn't work with the same
      > method if I can't pass values or if I pass a whitespace value:
      >[color=green][color=darkred]
      > >>> "".split()[/color][/color]
      > [][color=green][color=darkred]
      > >>> "".split(" ")[/color][/color]
      > ['']
      >
      > But into the doc I see:
      > """ If sep is not specified or is None, any whitespace string is a
      > separator.
      > """
      >
      > In this two cases, split would to return the _same_ result?[/color]

      split(None) strips the string before splitting it (on runs of whitespace,
      not on individual whitespace characters). see the library reference for
      a complete description:



      </F>



      Comment

      • Michele Petrazzo

        #4
        Re: String split

        Peter Otten wrote:[color=blue]
        > The documentation for Python 2.4 has a better explanation.
        >[/color]

        <-cut->
        [color=blue]
        > Quoted after http://docs.python.org/lib/string-methods.html#l2h-202.
        >
        > Peter[/color]

        Thanks, I haven't see it.
        Just a question about that "different algorithm", because it force the
        developer to do other work for make the "split" result more "logically
        compatible":

        S = "" # this can become from an external source like config parser

        for s n S.split():
        do the work... # don't go inside

        that isn't "compatible ", so python split it into two different methods
        the string, with:

        for s n S.split(','):
        do the work... # run one time into this


        Michele

        Comment

        • Peter Otten

          #5
          Re: String split

          Michele Petrazzo wrote:
          [color=blue]
          > Just a question about that "different algorithm", because it force the
          > developer to do other work for make the "split" result more "logically
          > compatible":
          >
          > S = "" # this can become from an external source like config parser
          >
          > for s n S.split():
          > do the work... # don't go inside
          >
          > that isn't "compatible ", so python split it into two different methods
          > the string, with:
          >
          > for s n S.split(','):
          > do the work... # run one time into this[/color]

          No question.
          [color=blue][color=green][color=darkred]
          >>> def split_any(s, seps):[/color][/color][/color]
          .... for sep in seps:
          .... parts = s.split(sep)
          .... if len(parts) > 1:
          .... return parts
          .... raise ValueError
          ....[color=blue][color=green][color=darkred]
          >>> split_any(" alpha beta ", [",", None])[/color][/color][/color]
          ['alpha', 'beta'][color=blue][color=green][color=darkred]
          >>> split_any("alph a,beta", [",", None])[/color][/color][/color]
          ['alpha', 'beta'][color=blue][color=green][color=darkred]
          >>> split_any("alph a_beta", [",", None])[/color][/color][/color]
          Traceback (most recent call last):
          File "<stdin>", line 1, in ?
          File "<stdin>", line 6, in split_any
          ValueError

          The answer :-)

          Seriously, I think you have to be a bit more explicit on what you want to
          know.

          Peter

          Comment

          Working...