Re: s.split() on multiple separators
- From: Tim Chase <python.list@xxxxxxxxxxxxxxxxx>
- Date: Sun, 30 Sep 2007 11:59:07 -0500
OK, so I want to split a string c into words using several different
separators from a list (dels).
I can do this the following C-like way:
cp=[]c=' abcde abc cba fdsa bcd '.split()
dels='ce '
for j in dels:
for i in xrange(0,len(c)-1):
cp.extend(c[i].split(j))
c=cp
['ab', 'd', '', 'ab', '', '']c
Given your original string, I'm not sure how that would be the
expected result of "split c on the characters in dels".
While there's a certain faction of pythonistas that don't esteem
regular expressions (or at least find them overused/misused,
which I'd certainly agree to), they may be able to serve your
purposes well:
>>> c=' abcde abc cba fdsa bcd '
>>> import re
>>> r = re.compile('[ce ]')
>>> r.split(c)
['', 'ab', 'd', '', 'ab', '', '', 'ba', 'fdsa', 'b', 'd', '']
given that a regexp object has a split() method.
-tkc
.
- Follow-Ups:
- Re: s.split() on multiple separators
- From: mrkafk
- Re: s.split() on multiple separators
- References:
- s.split() on multiple separators
- From: mrkafk
- s.split() on multiple separators
- Prev by Date: Re: Can you please give me some advice?
- Next by Date: Re: Can you please give me some advice?
- Previous by thread: Re: s.split() on multiple separators
- Next by thread: Re: s.split() on multiple separators
- Index(es):