[issue1123] split(None, maxplit) does not strip whitespace correctly
report at bugs.python.org
Fri Sep 7 03:18:41 CEST 2007
New submission from Nir Soffer:
string object .split doc say (http://docs.python.org/lib/string-
"If sep is not specified or is None, a different splitting algorithm
is applied. First, whitespace characters (spaces, tabs, newlines,
returns, and formfeeds) are stripped from both ends."
If the maxsplit argument is set and is smaller then the number of
possible parts, whitespace is not removed.
>>> 'k: v\n'.split(None, 1)
Expected: ['k:', 'v']
>>> u'k: v\n'.split(None, 1)
Expected: [u'k:', u'v']
With larger values of maxsplits, it works correctly:
>>> 'k: v\n'.split(None, 2)
>>> u'k: v\n'.split(None, 2)
This looks like implementation bug, because there it does not make sense
that the striping depends on the maxsplit argument, and it will be hard
to explain such behavior.
Maybe the striping should be removed in Python 3? It does not make sense
to strip a string behind your back when you want to split it, and the
caller can easily strip the string if needed.
components: Library (Lib)
title: split(None, maxplit) does not strip whitespace correctly
versions: Python 2.4, Python 2.5, Python 3.0
Tracker <report at bugs.python.org>
More information about the Python-bugs-list