By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,126 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,126 IT Pros & Developers. It's quick & easy.

Whitespace test after string.split

P: n/a
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
Nov 26 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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)
s = "alpha, \t\n, gamma, delta,"
s.split(",") ['alpha', ' \t\n', ' gamma', ' delta', ''] [t for t in s.split(",") if not t.isspace()] ['alpha', ' gamma', ' delta', ''] [t for t in s.split(",") if t and not t.isspace()] ['alpha', ' gamma', ' delta'] [t.strip() for t in s.split(",") if t and not t.isspace()]

['alpha', 'gamma', 'delta']

There you are.
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)?


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

Peter

Nov 26 '05 #2

P: n/a
Peter Otten wrote:
[t.strip() for t in s.split(",") if t and not t.isspace()]

['alpha', 'gamma', 'delta']


footnote: this solution is faster than my filter version.

</F>

Nov 26 '05 #3

P: n/a
On Sat, 26 Nov 2005 16:15:17 +0100, Peter Otten <__*******@web.de> wrote:
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)
s = "alpha, \t\n, gamma, delta,"
s.split(",")['alpha', ' \t\n', ' gamma', ' delta', ''] [t for t in s.split(",") if not t.isspace()]['alpha', ' gamma', ' delta', ''] [t for t in s.split(",") if t and not t.isspace()]['alpha', ' gamma', ' delta'] [t.strip() for t in s.split(",") if t and not t.isspace()]['alpha', 'gamma', 'delta']

There you are.

Alternatively, possibly
[t for t in (t.strip() for t in s.split(",")) if t]

['alpha', 'gamma', 'delta']
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)?


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

Peter


Regards,
Bengt Richter
Nov 27 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.