split() with a separator set

Igor V. Rafienko igorr at ifi.uio.no
Mon Jun 19 11:11:36 EDT 2000


Hi,

I've just started learning Python, so, please bear with me. 

string.split() function takes (amongst other things) a separator
argument, and the given string is split according to this separator.
But what if I have several one-character separators? Obviously:

# input looks like this: 'foo:bar|baz/zot'
# I want [ 'foo', 'bar', 'baz', 'zot' ]
string.split( src, ":|/" )

doesn't work.

I've written my own function to solve the problem:


import string

def splitsepset( src, sepset = string.whitespace ):
    reslist = []
    frompt = topt = 0
    inword = 0
    i = 0

    while i < len( src ):
        if src[i] in sepset:
            if inword:
                reslist.append( src[ frompt:topt ] )
                inword = 0
            frompt = topt = i+1
        else :
            inword = 1
            topt = topt + 1
        
        i = i + 1
    # end while

    return reslist
# end mysplit


but it seems just too awkward to use that.

Is there a better (that is first cleaner, then faster) way to split a
string into fields according to a one-character separator _set_ (for
instance, as string.split( s ) does, except that I want to specify
something else than string.whitespace)? And as a follow-up -- how do I
split a string according to a set of _multicharacter_ separators?


Thanks in advance,



ivr, willing to read the proper section of the FAQ.
-- 
Simula-konsulent? Simsulent? "I eat Java for breakfast!".
                                           -- thorkild



More information about the Python-list mailing list