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

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 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.

Python, 9 lines
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
>>>

5 comments

Gabriel Genellina 14 years, 2 months ago  # | flag

Doesn't work for me:

mpartition("Now the world is gone, I'm just one. Oh, God, help me! Hold my breath as I wish for death. Oh, please God, help me!", ".", ",")
["Now the world is gone, I'm just one",
 '.',
 ' Oh',
 ',',
 ' God, help me! Hold my breath as I wish for death. Oh, please God, help me!']

mpartition("A.B,C.D,E.F", ".", ",")
['A', '.', 'B', ',', 'C.D,E.F']

mpartition("A,B.C", ".", ",")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in mpartition
ValueError: Substring ',' not found
Michael Grünewald (author) 14 years, 2 months ago  # | flag

@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 or split 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.

Gabriel Genellina 14 years, 2 months ago  # | flag

Look at my second example, the delimiters occur in the right sequence and still doesn't work.

Michael Grünewald (author) 14 years, 2 months ago  # | flag

Actually no, the delimiters do not occur in the right sequence. Try mpartition("A,B.C", ",", ".") instead of mpartition("A,B.C", ".", ","). That should work.

Gabriel Genellina 14 years, 2 months ago  # | flag

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.

Created by Michael Grünewald on Thu, 21 Jan 2010 (MIT)
Python recipes (4591)
Michael Grünewald's recipes (7)

Required Modules

  • (none specified)

Other Information and Tasks