Welcome, guest | Sign In | My Account | Store | Cart

Splits strings with multiple separators instead of one (e.g. str.split()).

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def tsplit(string, delimiters):
    """Behaves str.split but supports multiple delimiters."""
    
    delimiters = tuple(delimiters)
    stack = [string,]
    
    for delimiter in delimiters:
        for i, substring in enumerate(stack):
            substack = substring.split(delimiter)
            stack.pop(i)
            for j, _substring in enumerate(substack):
                stack.insert(i+j, _substring)
            
    return stack

Many times I've wanted to split a string with multiple separators.

Example:

>>> s = 'thing1,thing2/thing3-thing4'
>>> tsplit(s, (',', '/', '-'))
['thing1', 'thing2', 'thing3', 'thing4']

6 comments

Sunjay Varma 10 years, 8 months ago  # | flag

How about this:

def tsplit(s, sep):
    stack = [s]
    for char in sep:
        pieces = []
        for substr in stack:
            pieces.extend(substr.split(char))
        stack = pieces
    return stack

This version allows for a more flexible separator which can be a string, tuple, or list.

>>> tsplit("hello,my|name+is-bob", (",", "|", "+", "-"))
['hello', 'my', 'name', 'is', 'bob']
>>> tsplit("hello,my|name+is-bob", ",|+-")
['hello', 'my', 'name', 'is', 'bob']
>>> tsplit("hello,my|name+is-bob", list(",|+-"))
['hello', 'my', 'name', 'is', 'bob']
>>> tsplit("hello,my|name+is-bob", "+")
['hello,my|name', 'is-bob']

-Sunjay03

Kenneth Reitz (author) 10 years, 8 months ago  # | flag

@sunjay, I like that, but sometimes you want to split by more than one character at a time.

Kent Johnson 10 years, 8 months ago  # | flag

re.split() is simpler:

import re
def tsplit(string, *delimiters):
    pattern = '|'.join(map(re.escape, delimiters))
    return re.split(pattern, string)

or just use re.split() directly:

re.split(',|/|-', s)
['thing1', 'thing2', 'thing3', 'thing4']
Denis Ignatenko 10 years, 8 months ago  # | flag

@Kent in re.split you have to escape special regexp characters like . \ *

Kent Johnson 10 years, 8 months ago  # | flag

@Denis - Yes, that is what map(re.escape, delimiters) does in my tsplit() function. Of course if you are calling re.split() directly you have to give a correct regex yourself.

Stephen Chappell 10 years, 8 months ago  # | flag

@Johnson - Thanks for the four line solution that uses re!

Created by Kenneth Reitz on Sun, 20 Mar 2011 (MIT)
Python recipes (4591)
Kenneth Reitz's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks