By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

strtok equvialent ?

P: n/a
Hi,

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

Nov 3 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
bo****@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>

Nov 3 '05 #2

P: n/a
thanks.

Fredrik Lundh wrote:
bo****@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>


Nov 3 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.