Split on multiple delimiters, and also treat consecutive delimiters as a single delimiter?
Rustom Mody
rustompmody at gmail.com
Tue Jul 28 22:41:55 EDT 2015
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']
More information about the Python-list
mailing list