Whitespace test after string.split

Fredrik Lundh fredrik at pythonware.com
Sat Nov 26 10:19:16 EST 2005


David Pratt wrote:

> 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)

call strip, and only include the string in the result if it's not empty.  combining
a generator expression with the filter() function should be rather efficient:

    >>> text = "blah blah  \n: : blah   ::blah::::"
    >>> print filter(None, (x.strip() for x in text.split(":")))
    ['blah blah', 'blah', 'blah']

in 2.3 and earlier, use

    >>> print filter(None, [x.strip() for x in text.split(":")])

instead.

(this assumes that you don't want to keep empty items either)

> 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)?

the "string.split" function in the "string" module is outdated; the "split"
method on string objects is not.

</F> 






More information about the Python-list mailing list