[Tutor] tokenizing a simple string with split()
David Heiser
David.Heiser at intelliden.com
Sun Apr 1 08:43:34 CEST 2007
Or you can try something like:
x = r"C:\My\Doc\;D:\backup"
x = x.replace("\\", ";")
x = x.split(";")
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Kent Johnson
Sent: Saturday, March 31, 2007 9:42 PM
To: Andrei Petre
Cc: tutor at python.org
Subject: Re: [Tutor] tokenizing a simple string with split()
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".
The argument to split() is the literal string to split on, not a list of
potential splitting characters. So to split on '; ' your string would
have to be 'spam; egg; mail'.
To split on one of a list of characters you have to use a regular
expression and re.split().
In [1]: import re
In [3]: re.split('[; ]', "spam;egg mail")
Out[3]: ['spam', 'egg', 'mail']
[; ] is a regular expression that means, "match either of ; or space".
Kent
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list