Splitting on '^' ?
Brian
Brian.Mingus at colorado.edu
Fri Aug 14 16:43:45 EDT 2009
On Fri, Aug 14, 2009 at 2:23 PM, kj <no.email at please.post> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>
You shouldn't use a regular expression for that.
>>> from time import time
>>> start=time();'spam\nham\neggs\n'.split('\n');print time()-start;
['spam', 'ham', 'eggs', '']
4.6968460083e-05
>>> import re
>>> start=time();re.split(r'\n', 'spam\nham\neggs');print time()-start;
['spam', 'ham', 'eggs']
0.000284910202026
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090814/c8ff02a8/attachment-0001.html>
More information about the Python-list
mailing list