[Python-ideas] Optional keepsep argument in str.split()

Jared Grubb jared.grubb at gmail.com
Thu Aug 29 07:49:21 CEST 2013


On Aug 28, 2013, at 12:40, Marco Buttu <mbuttu at oa-cagliari.inaf.it> wrote:

> On 08/28/2013 08:57 PM, MRAB wrote:
>> On 28/08/2013 19:42, Marco Buttu wrote: 
>>> What do you think about an optional `keepsep` argument in str.split(), 
>>> in order to keep the separator? 
>>> Something like the `keepends` of str.splitlines(): 
>>> 
>>>       >>> 'I am\ngoing\nto...'.splitlines(keepends=True) 
>>>       ['I am\n', 'going\n', 'to...'] 
>>> 
>>> For instance: 
>>> 
>>>       >>> 'python3'.split('n') 
>>>       ['pytho', '3'] 
>>>       >>> 'python3'.split('n', keepsep=True) 
>>>       ['python', '3'] 
>>> 
>> If it's a _separator_, should it be attached to the previous part? 
>> Shouldn't it be: 
>> 
>> >>> 'python3'.split('n', keepsep=True) 
>> ['pytho', 'n', '3'] 
> 
> It should be attached to the previous part, exactly as my example
> 
>> 
>> What's your use-case?
> 
> I think it could be useful in a lot of use-cases, when you have to parse a string. For instance,
> if you have some source code, and you want to write it better:
> 
> >>> source_code = "int a = 33;cout << a << endl;return 0;"
> >>> print('\n'.join(source_code.split(';')))
> int a = 33
> cout << a << endl
> return 0
> 
> >>> print('\n'.join(source_code.split(';', keepsep=True)))
> int a = 33;
> cout << a << endl;
> return 0;

Split and join are inverses, so it's lossless. You get this behavior by putting the semicolon in:

>>> print(';\n'.join(source_code.split(';')))
int a = 33;
cout << a << endl;
return 0;

So I'm not sure this particular use-case is compelling.

Jared

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130828/1a4f4396/attachment.html>


More information about the Python-ideas mailing list