strtok equvialent ?

Fredrik Lundh fredrik at pythonware.com
Thu Nov 3 06:11:38 EST 2005


bonono at 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> 






More information about the Python-list mailing list