473,396 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

String split

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
4 6013
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
11
by: Carlos Ribeiro | last post by:
Hi all, While writing a small program to help other poster at c.l.py, I found a small inconsistency between the handling of keyword parameters of string.split() and the split() method of...
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
6
by: Senthil | last post by:
Code ---------------------- string Line = "\"A\",\"B\",\"C\",\"D\""; string Line2 = Line.Replace("\",\"","\"\",\"\""); string CSVColumns = Line2.Split("\",\"".ToCharArray());
19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
3
by: Hetal Shah | last post by:
It is a C# code. I have a string like, one||two||three I want to split it into one, two and three. string str = "one||two||three"; string myStrs = str.Split("||");
4
by: Crirus | last post by:
There is a function somewhere to split a string with multiple tokens at a time? Say I have this: aaaa#bbbbb*ccccc$dddd I whould like to split it so the result whould be aaaa bbb
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine, vbCr, vbLf) Dim csvColumns2 As String() =...
2
by: Digital Fart | last post by:
following code would split a string "a != b" into 2 strings "a" and "b". but is there a way to know what seperator was used? string charSeparators = { "=", ">=", "<=" , "!=" }; string s1 =...
12
by: John | last post by:
Hi I have a multi-line address field which has each line separated by CRLF. How can I split this field into individual strings using crlf as separator? Thanks Regards
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.