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:
split off ’11’, do something with it
split off ’22’, do something with it
split off ’33’, do something with it
split off ‘BEGINxx44xx55xxEND’, do something with it
split off ’66’, do something with it
split off ’77’, do something with it
I tried a recursive function:
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)
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.
Thanks!
You can do it with a regular expression:
Result:
See it working online: ideone