strtok equvialent ?

B

bonono

Hi,

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

Fredrik Lundh

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>
 
B

bonono

thanks.

Fredrik said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Access violation reading location 0
Can't solve problems! please Help 0
strtok() 13
C++ strtok 5
strtok and strsep 6
strtok 10
Best way to tokenize in String 7
strtok segfaults in CLI but not in GDB 21

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top