Splitting on '^' ?

Ethan Furman ethan at stoneleaf.us
Fri Aug 14 17:20:06 EDT 2009


kj wrote:
> 
> 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.:
> 
> 
>>>>import re
>>>>re.split('^', 'spam\nham\neggs\n')
> 
> ['spam\nham\neggs\n']
> 
>>>>re.split('(?m)^', 'spam\nham\neggs\n')
> 
> ['spam\nham\neggs\n']
> 
>>>>bol_re = re.compile('^', re.M)
>>>>bol_re.split('spam\nham\neggs\n')
> 
> ['spam\nham\neggs\n']
> 
> Am I doing something wrong?
> 
> kynn

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
        result.pop()
     else:                   # or extra sep from last element
         result[-1] = result[-1][:-len(sep)]
     return result



More information about the Python-list mailing list