Python strtok equvialent

Thursday, June 2, 2011

C programmers usually use strtok to perform operation on string with standing patterns. In Python, we can use re.split to get similar result.

Example:

>>> import re
>>> st = "9394 811 813 815"
>>> result = [str(x) for x in filter(None, re.split('[ ,\n,\t]',st))]
>>> print result
['9394', '811', '813', '815']
>>>