Splitting on '^' ?
no.email at please.post
Sun Aug 16 19:09:32 CEST 2009
In <ad212ad9-2cac-4a03-a25d-1d46dd527036 at w41g2000yqb.googlegroups.com> rurpy at yahoo.com writes:
>On Aug 14, 2:23=A0pm, kj <no.em... at please.post> wrote:
>> Sometimes I want to split a string into lines, preserving the
>> end-of-line markers. =A0In Perl this is really easy to do, by splitting
>> on the beginning-of-line anchor:
>> =A0 @lines =3D split /^/, $string;
>> But I can't figure out how to do the same thing with Python. =A0E.g.:
>Why not this?
>>>> lines =3D 'spam\nham\neggs\n'.splitlines (True)
>['spam\n', 'ham\n', 'eggs\n']
And .splitlines seems to be able to handle all "standard" end-of-line
markers without any special direction (which, ironically, strikes
me as a *little* Perlish, somehow):
['spam\r\n', 'ham\r', 'eggs\n']
Amazing. I'm not sure this is the *best* way to do this in general
(I would have preferred it, and IMHO it would have been more
Pythonic, if .splitlines accepted an additional optional argument
where one could specify the end-of-line sequence to be used for
the splitting, defaulting to the OS's conventional sequence, and
then it split *strictly* on that sequence).
But for now this .splitlines will do nicely.
More information about the Python-list