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

Marco Buttu mbuttu at oa-cagliari.inaf.it
Thu Aug 29 11:25:55 CEST 2013


On 08/28/2013 09:44 PM, Ryan Gonzalez wrote:
> Sounds interesting...not sure about how often it'd be used, since I 
> could always use re:
>
> re.split('(n)', 'python3')

It is not the same. As I wrote in the first message, the separator have 
to be attached at its token, in the
same way the srt.splitlines() `keepends` argument works:

 >>> data = "{1: 'one', 2: 'two'}{3: 'three', 4: 'four'}"
 >>> import re
 >>> for item in re.split('(})', data):
...     print(item)
...
{1: 'one', 2: 'two'
}
{3: 'three', 4: 'four'
}

 >>> for item in data.split(sep='}', keepsep=True):
...     print(item)
...
{1: 'one', 2: 'two'}
{3: 'three', 4: 'four'}

Regards, Marco

-- 
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



More information about the Python-ideas mailing list