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

Marco Buttu mbuttu at oa-cagliari.inaf.it
Wed Aug 28 21:40:01 CEST 2013


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;

-- 
Marco Buttu

INAF Osservatorio Astronomico di Cagliari
Loc. Poggio dei Pini, Strada 54 - 09012 Capoterra (CA) - Italy
Phone: +39 070 71180255
Email: mbuttu at oa-cagliari.inaf.it

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


More information about the Python-ideas mailing list