String split

M

Michele Petrazzo

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:
['']

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
 
P

Peter Otten

Michele said:
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
 
F

Fredrik Lundh

Michele said:
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>
 
M

Michele Petrazzo

Peter said:
The documentation for Python 2.4 has a better explanation.


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
 
P

Peter Otten

Michele said:
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.
.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top