Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter?
Oscar Benjamin
oscar.j.benjamin at gmail.com
Tue Jul 28 10:09:09 EDT 2015
On Tue, 28 Jul 2015 at 15:01 Victor Hooi <victorhooi at gmail.com> wrote:
> I have a line that looks like this:
>
> 14 *0 330 *0 760 411|0 0 770g 1544g 117g
> 1414 computedshopcartdb:103.5% 0 30|0 0|1 19m 97m
> 1538 ComputedCartRS PRI 09:40:26
>
> I'd like to split this line on multiple separators - in this case,
> consecutive whitespace, as well as the pipe symbol (|).
>
Is this what you want:
In [5]: def split(s):
...: elements = []
...: for x in s.split(): # Split whitespace
...: elements.extend(x.split('|'))
...: return elements
...:
In [6]: s = "14 *0 330 *0 760 411|0 0 770g
1544g 117g 1414 computedshopcartdb:103.5% 0 30|0
0|1 19m 97m 1538 ComputedCartRS PRI 09:40:26"
In [7]: split(s)
Out[7]:
['14',
'*0',
'330',
'*0',
'760',
'411',
'0',
'0',
'770g',
'1544g',
'117g',
'1414',
'computedshopcartdb:103.5%',
'0',
'30',
'0',
'0',
'1',
'19m',
'97m',
'1538',
'ComputedCartRS',
'PRI',
'09:40:26']
--
Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150728/3e86893a/attachment.html>
More information about the Python-list
mailing list