[issue7638] Counterintuitive str.splitlines() inconsistency.

R. David Murray report at bugs.python.org
Tue Jan 5 05:55:15 CET 2010


R. David Murray <rdmurray at bitdance.com> added the comment:

No apologies needed, but you probably aren't going to like the answer :)

First of all, a change like you propose would be unlikely to be accepted since it would create considerable backward-compatibility pain.

That aside, however, splitlines and split are not meant to be parallel.  They do two very different jobs.  splitlines is *line* oriented, and lines are understood to end with line ends.  The file equivalent of "" has length zero, and the unix 'wc' command reports it has having 0 lines.  A file containing "derp\n' is reported by wc to have one line, not two.  Files without a final line end are arguably broken, but all good tools accept that final line as a line, though some complain about it.  (And other tools break in various odd ways.)

If you want something parallel to split that handles line ends 'universally', try re.split with an appropriate regex.

----------
nosy: +r.david.murray
priority:  -> normal
resolution:  -> invalid
stage:  -> committed/rejected
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7638>
_______________________________________


More information about the Python-bugs-list mailing list