howto split string with both comma and semicolon delimiters

Gary Herron gherron at islandtraining.com
Thu Jun 12 14:18:33 EDT 2008


dmitrey wrote:
> hi all,
> 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
> Thx, D.
> --
> http://mail.python.org/mailman/listinfo/python-list
>   

The regular expression module has a split function that does what you 
want. 

 >>> import re
 >>> r =',|;'   # or this also works: '[,;]'
 >>> s = "a,b;c"
 >>> re.split(r,s)
['a', 'b', 'c']


Gary Herron





More information about the Python-list mailing list