Whitespace test after string.split

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Pratt

    #1

    Whitespace test after string.split

    Hi. I am splitting a string on a non whitespace character. One or more
    whitespace characters can be returned as items in the list. I do not
    want the items in the list that are only whitespace (can be one or more
    characters of whitespace) and plan to use string.strip on those items
    that are not only whitespace (to remove any whitespace from front or
    back of items).

    What kind of efficient test can I use to obtain only list items
    returned from the split that I am interested in, ignoring any list
    items that would only be comprised of one or more characters of
    whitespace (since whitespace can mean one or more spaces, tabs, and
    other characters)

    As a second question, I am seeing string split as deprecated in 2.4.2
    manual. What is planned in future to split (strings or unicode)?

    Regards,
    David
  • Peter Otten

    #2
    Re: Whitespace test after string.split

    David Pratt wrote:
    [color=blue]
    > Hi. I am splitting a string on a non whitespace character. One or more
    > whitespace characters can be returned as items in the list. I do not
    > want the items in the list that are only whitespace (can be one or more
    > characters of whitespace) and plan to use string.strip on those items
    > that are not only whitespace (to remove any whitespace from front or
    > back of items).
    >
    > What kind of efficient test can I use to obtain only list items
    > returned from the split that I am interested in, ignoring any list
    > items that would only be comprised of one or more characters of
    > whitespace (since whitespace can mean one or more spaces, tabs, and
    > other characters)[/color]
    [color=blue][color=green][color=darkred]
    >>> s = "alpha, \t\n, gamma, delta,"
    >>> s.split(",")[/color][/color][/color]
    ['alpha', ' \t\n', ' gamma', ' delta', ''][color=blue][color=green][color=darkred]
    >>> [t for t in s.split(",") if not t.isspace()][/color][/color][/color]
    ['alpha', ' gamma', ' delta', ''][color=blue][color=green][color=darkred]
    >>> [t for t in s.split(",") if t and not t.isspace()][/color][/color][/color]
    ['alpha', ' gamma', ' delta'][color=blue][color=green][color=darkred]
    >>> [t.strip() for t in s.split(",") if t and not t.isspace()][/color][/color][/color]
    ['alpha', 'gamma', 'delta']

    There you are.
    [color=blue]
    > As a second question, I am seeing string split as deprecated in 2.4.2
    > manual. What is planned in future to split (strings or unicode)?[/color]

    Just use the corresponding methods, e. g. s.strip() instead of
    string.strip(s) etc.

    Peter

    Comment

    • Fredrik Lundh

      #3
      Re: Whitespace test after string.split

      Peter Otten wrote:
      [color=blue][color=green][color=darkred]
      >>>> [t.strip() for t in s.split(",") if t and not t.isspace()][/color][/color]
      > ['alpha', 'gamma', 'delta'][/color]

      footnote: this solution is faster than my filter version.

      </F>



      Comment

      • Bengt Richter

        #4
        Re: Whitespace test after string.split

        On Sat, 26 Nov 2005 16:15:17 +0100, Peter Otten <__peter__@web. de> wrote:
        [color=blue]
        >David Pratt wrote:
        >[color=green]
        >> Hi. I am splitting a string on a non whitespace character. One or more
        >> whitespace characters can be returned as items in the list. I do not
        >> want the items in the list that are only whitespace (can be one or more
        >> characters of whitespace) and plan to use string.strip on those items
        >> that are not only whitespace (to remove any whitespace from front or
        >> back of items).
        >>
        >> What kind of efficient test can I use to obtain only list items
        >> returned from the split that I am interested in, ignoring any list
        >> items that would only be comprised of one or more characters of
        >> whitespace (since whitespace can mean one or more spaces, tabs, and
        >> other characters)[/color]
        >[color=green][color=darkred]
        >>>> s = "alpha, \t\n, gamma, delta,"
        >>>> s.split(",")[/color][/color]
        >['alpha', ' \t\n', ' gamma', ' delta', ''][color=green][color=darkred]
        >>>> [t for t in s.split(",") if not t.isspace()][/color][/color]
        >['alpha', ' gamma', ' delta', ''][color=green][color=darkred]
        >>>> [t for t in s.split(",") if t and not t.isspace()][/color][/color]
        >['alpha', ' gamma', ' delta'][color=green][color=darkred]
        >>>> [t.strip() for t in s.split(",") if t and not t.isspace()][/color][/color]
        >['alpha', 'gamma', 'delta']
        >
        >There you are.[/color]
        Alternatively, possibly
        [color=blue][color=green][color=darkred]
        >>> [t for t in (t.strip() for t in s.split(",")) if t][/color][/color][/color]
        ['alpha', 'gamma', 'delta']
        [color=blue]
        >[color=green]
        >> As a second question, I am seeing string split as deprecated in 2.4.2
        >> manual. What is planned in future to split (strings or unicode)?[/color]
        >
        >Just use the corresponding methods, e. g. s.strip() instead of
        >string.strip(s ) etc.
        >
        >Peter
        >[/color]

        Regards,
        Bengt Richter

        Comment

        Working...