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

String split

P: n/a
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:
"".split() [] "".split(" ")

['']

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
Mar 28 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Michele Petrazzo wrote:
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:
"".split() [] "".split(" ")

['']

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


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
Mar 28 '06 #2

P: n/a
Michele Petrazzo wrote:
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:
"".split() [] "".split(" ")

['']

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?


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:

http://docs.python.org/lib/string-methods.html

</F>

Mar 28 '06 #3

P: n/a
Peter Otten wrote:
The documentation for Python 2.4 has a better explanation.

<-cut->
Quoted after http://docs.python.org/lib/string-methods.html#l2h-202.

Peter


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
Mar 28 '06 #4

P: n/a
Michele Petrazzo wrote:
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


No question.
def split_any(s, seps): .... for sep in seps:
.... parts = s.split(sep)
.... if len(parts) > 1:
.... return parts
.... raise ValueError
.... split_any(" alpha beta ", [",", None]) ['alpha', 'beta'] split_any("alpha,beta", [",", None]) ['alpha', 'beta'] split_any("alpha_beta", [",", None])

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

Mar 28 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.