Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94716

Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter?

Newsgroups comp.lang.python
Date 2015-07-28 19:41 -0700
References <fed7bab5-db18-45c3-9ba2-4b7fbfa80602@googlegroups.com> <mailman.1055.1438132534.3674.python-list@python.org>
Message-ID <153c2ad1-a5c7-4ac0-86a4-0e62cd2a92cd@googlegroups.com> (permalink)
Subject Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter?
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


On Wednesday, July 29, 2015 at 6:45:45 AM UTC+5:30, Chris Angelico wrote:
> On Tue, Jul 28, 2015 at 11:55 PM, Victor Hooi  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 (|).
> 
> Correct me if I'm misanalyzing this, but it sounds to me like a simple
> transform-then-split would do the job:
> 
> f.replace("|"," ").split()
> 
> Turn those pipe characters into spaces, then split on whitespace. Or,
> reading it differently: Declare that pipe is another form of
> whitespace, then split on whitespace. Python lets you declare anything
> you like, same as mathematics does :)

I dont see how anything can beat MRABs in declarativeness, neatness succinctness


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"

>>> split('[ |]+', 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']

And if you dont mind two steps here is another longer-looking but more straightforward (IMHO of course):
>>> [z for y in s.split() for z in y.split('|')]
['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']

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? Victor Hooi <victorhooi@gmail.com> - 2015-07-28 06:55 -0700
  Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? m <mvoicem@gmail.com> - 2015-07-28 15:59 +0200
    Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? Victor Hooi <victorhooi@gmail.com> - 2015-07-28 07:09 -0700
      Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? MRAB <python@mrabarnett.plus.com> - 2015-07-28 15:30 +0100
  Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? Chris Angelico <rosuav@gmail.com> - 2015-07-29 10:08 +1000
    Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? Rustom Mody <rustompmody@gmail.com> - 2015-07-28 19:41 -0700
  Re: Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter? Joel Goldstick <joel.goldstick@gmail.com> - 2015-07-28 21:28 -0400

csiph-web