Works like the
partition method of strings in Python2.5+, but has support for more than one delimiter.
Better description by Gabriel Genellina:
Split the string at separator boundaries. The separators are searched from left to right, in the same order specified, and one after another. Unlike
partition, all of them must be present (else
ValueErroris raised). Only one split per separator occurrence is done. Returns a list containing one more element than separators were given: first, text from beginning of the string up to (but not including) the first separator; the first separator itself; text between the first separator and the second one; the second separator; and so on. The last element contains all text following the last separator.
1 2 3 4 5 6 7 8 9
def mpartition(s, *substrings): idx = 0 splits = [0, None] for substring in substrings: idx = s.find(substring, idx) if idx == -1: raise ValueError, "Substring %r not found" % substring splits[-1:-1] = [idx, idx+len(substring)] return [s[splits[i]:splits[i+1]] for i in range(len(splits)-1)]
>>> mpartition("The $obj count is $num.", "$obj", "$num") ['The ', '$obj', ' count is ', '$num', '.'] >>> mpartition("It can fail.", "must") Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 7, in mpartition ValueError: Substring 'must' not found >>>