469,579 Members | 1,344 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

howto split string with both comma and semicolon delimiters

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.
Jun 27 '08 #1
4 8730
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
Jun 27 '08 #2
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
Jun 27 '08 #3
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.
Howabout:

s = s.replace(";", ",")
s = s.split(",")

Jun 27 '08 #4
On Jun 12, 8:06 pm, bvdp <b...@mellowood.cawrote:
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.

Howabout:

s = s.replace(";", ",")
s = s.split(",")
I've wondered in the past whether there would be sufficient need for
things like s.split((',', ';')) and s.partition((',', ';')).
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by William Stacey [MVP] | last post: by
6 posts views Thread by Senthil | last post: by
8 posts views Thread by mannyGonzales | last post: by
5 posts views Thread by kurt sune | last post: by
7 posts views Thread by lgbjr | last post: by
14 posts views Thread by tom t/LA | last post: by
4 posts views Thread by Tem | last post: by
10 posts views Thread by teddyber | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.