howto split string with both comma and semicolon delimiters

Daniel Fetchinson fetchinson at googlemail.com
Thu Jun 12 20:25:54 CEST 2008


> howto split string with both comma and semicolon delimiters?
>
> i.e. (for example) get ['a','b','c'] from string "a,b;c"
>
> I have tried s.split(',;') but it don't work

A very pedestrian solution would be:

def multisplit( s, seps ):

    words = [ ]
    word = ''
    for char in s:
        if char in seps:
            if word:
                words.append( word )
            word = ''
        else:
            word += char

    if word:
        words.append( word )

    return words


Cheers,
Daniel
-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list