Skip to content

Instantly share code, notes, and snippets.

@mrtazz
Forked from kennethreitz/tsplit.py
Created March 21, 2011 09:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtazz/879213 to your computer and use it in GitHub Desktop.
Save mrtazz/879213 to your computer and use it in GitHub Desktop.
def tsplit(string, delimiters):
"""Behaves str.split but supports multiple delimiters."""
delimiters = tuple(delimiters)
stack = [""] # stack to return
for i in xrange(len(string)):
if string[i] in delimiters:
stack.append("")
else:
stack[-1] += string[i]
return stack
>>> s = 'thing1,thing2/thing3-thing4'
>>> tsplit(s, (',', '/', '-'))
['thing1', 'thing2', 'thing3', 'thing4']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment