Splitting on '^' ?
ethan at stoneleaf.us
Fri Aug 14 23:20:06 CEST 2009
> Sometimes I want to split a string into lines, preserving the
> end-of-line markers. In Perl this is really easy to do, by splitting
> on the beginning-of-line anchor:
> @lines = split /^/, $string;
> But I can't figure out how to do the same thing with Python. E.g.:
>>>>bol_re = re.compile('^', re.M)
> Am I doing something wrong?
As you probably noticed from the other responses: No, you can't split
on _and_ keep the splitby text.
Looks like you'll have to roll your own.
def splitat(text, sep):
result = [line + sep for line in text.split(sep)]
if result[-1] == sep: # either remove extra element
else: # or extra sep from last element
result[-1] = result[-1][:-len(sep)]
More information about the Python-list