[Tutor] tokenizing a simple string with split()

Rafael Garcia rgarcia at fas.harvard.edu
Sun Apr 1 00:00:47 CEST 2007


I think by "multiple characters" they mean more than one character for 
ONE separator. I don't know how to specify multiple separators. You 
could try something like this:

s = "spam;egg mail"
list = []
for t in s.split(";"):
    for u in t.split(" "):
        list.append(u)

which yields:
list = ['spam', 'egg', 'mail']


Rafael

On 3/31/07 5:43 PM, Andrei Petre wrote:
> I want to split a string like "C:\My\Doc\;D:\backup\" with two 
> separators: \ and ;
> I found that \ is handled with /raw string/ notation r"". But the 
> problem i encountered is with split() function.
> In the 2.5 reference is said that "The sep argument of the split() 
> function may consist of multiple characters". But I cannot figured out 
> why this simple example not working:
>
> s = "spam;egg mail"
> s.split("; ")
>
> output: ['spam;egg mail']
>
> instead of ['spam', 'egg', 'mail']
>
> any suggestion is welcome,
> andrei
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070331/8e1bedb4/attachment.htm 


More information about the Tutor mailing list