By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,925 Members | 727 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,925 IT Pros & Developers. It's quick & easy.

howto split string with both comma and semicolon delimiters

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.