strtok equvialent ?

1 view
Skip to first unread message

bon...@gmail.com

unread,
Nov 3, 2005, 5:59:13 AM11/3/05
to
Hi,

are there a strtok equivalent in python ? str.split() only takes single
seperator.

Fredrik Lundh

unread,
Nov 3, 2005, 6:11:38 AM11/3/05
to pytho...@python.org
bon...@gmail.com wrote:

> are there a strtok equivalent in python ? str.split() only takes single
> seperator.

use a regular expression split with a character group:

>>> s = "breakfast=spam+egg-bacon"
>>> import re
>>> re.split("[-+=]", s)
['breakfast', 'spam', 'egg', 'bacon']

to deal with an arbitrary set of delimiting characters without having to
bother with RE syntax, use re.escape:

>>> re.split("[" + re.escape("-+=") + "]", s)
['breakfast', 'spam', 'egg', 'bacon']

</F>

bon...@gmail.com

unread,
Nov 3, 2005, 6:29:55 AM11/3/05
to
thanks.
Reply all
Reply to author
Forward
0 new messages