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 (elseValueError
is 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)]
|
Usage:
>>> 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
>>>
Doesn't work for me:
@Gabriel Genellina: Yes, it (intentionally) only works when the delimiters are in the correct order. It's not for general text splitting (use the
re
module orsplit
for that) but rather for splitting string like those in my example, i.e. strings where one knows that the delimiters occur in a specific order and number.Look at my second example, the delimiters occur in the right sequence and still doesn't work.
Actually no, the delimiters do not occur in the right sequence. Try
mpartition("A,B.C", ",", ".")
instead ofmpartition("A,B.C", ".", ",")
. That should work.Ahhhhhhhhh, ok! I completely misunderstood what this recipe does, it's clear now. I was expecting mpartition("A.B,C.D,E.F", ".", ",") to return ['A','.','B',',','C','.' ... 'F'] instead.
I think a better description might be: 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 ValueError is 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.