FAQ

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

Search Discussions

Discussion Posts

Previous

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 8 of 8 | next ›
Discussion Overview
grouppython-list @
categoriespython
postedJul 28, '15 at 1:55p
activeJul 29, '15 at 2:41a
posts8
users7
websitepython.org

People

Translate

site design / logo © 2018 Grokbase