New submission from Barry A. Warsaw:
This has finally bugged me enough to file an issue, although I wouldn't be able
to use it until Python 3.7. There's a subtle but documented difference in
str.split() when sep=None:
>>> help(''.split)
Help on built-in function split:
split(...) method of builtins.str instance
S.split(sep=None, maxsplit=-1) -> list of strings
Return a list of the words in S, using sep as the
delimiter string. If maxsplit is given, at most maxsplit
splits are done. If sep is not specified or is None, any
whitespace string is a separator and empty strings are
removed from the result.
I.e., that empty strings are removed from the result. This does not happen
when sep is given, leading to this type of unfortunate code:
>>> 'foo,bar,baz'.split(',')
['foo', 'bar', 'baz']
>>> 'foo,bar,baz'.replace(',', ' ').split()
['foo', 'bar', 'baz']
>>> ''.split(',')
['']
>>> ''.replace(',', ' ').split()
[]
Specifically, code that wants to split on say commas, but has to handle the
case where the source string is empty, shouldn't have to also filter out the
single empty string item.
Obviously we can't change existing behavior, so I propose to add a keyword
argument `prune` that would make these two bits of code identical:
>>> ''.split()
[]
>>> ''.split(' ', prune=True)
[]
and would handle the case of ''.split(',') without having to resort to creating
an ephemeral intermediate string.
`prune` should be a keyword-only argument, defaulting to False.
----------
components: Library (Lib)
messages: 282923
nosy: barry
priority: normal
severity: normal
status: open
title: str.split(): remove empty strings when sep is not None
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue28937>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com