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

Chris Angelico rosuav at gmail.com
Wed Jul 29 02:08:21 CEST 2015


On Tue, Jul 28, 2015 at 11:55 PM, 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 (|).

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

ChrisA


More information about the Python-list mailing list