python: conditinoally splitting a string
<p>there are lots of questions about split in python, but I couldn't find
corresponding to my problem. I want to split a string, but need to have
different values for the splitter, depending on a condition. For the test
case, my string is "11xx22xx33xxBEGINxx44xx55xxENDxx66xx77". I want to
process this string in chunks, meaning I want to step through it like
this:</p>

<p>split off '11', do something with it</p>

<p>split off '22', do something with it</p>

<p>split off '33', do something with it</p>

<p>split off 'BEGINxx44xx55xxEND', do something with it</p>

<p>split off '66', do something with it</p>

<p>split off '77', do something with it</p>

<p>I tried a recursive function:</p>

<pre><code>import string

mystring = "11xx22xx33xxBEGINxx44xx55xxENDxx66xx77"

def makechunks(s):
    try: splitter
    except NameError:
        splitter = "xx"
    whole = s.split(splitter, 1)
    current = whole[0]
    try: whole[1]
    except NameError:
        return
    else:
        rest = whole[1]
        if current.find("BEGIN", 0, 5):
            splitter = "END"
        else:
            splitter = "xx"
        makechunks(rest)
        print("AA", current, "BB")

makechunks(mystring)
</code></pre>

<p>But I'm getting the error "list index out of range." Maybe my entire
approach is flawed, and there are better ways to achieve what I want? I'll
be grateful for any hint.</p>

<p>Thanks!</p>
