multi split function taking delimiter list
Raymond Hettinger
python at rcn.com
Tue Nov 14 15:16:09 EST 2006
martinskou at gmail.com wrote:
> Hi, I'm looking for something like:
>
> multi_split( 'a:=b+c' , [':=','+'] )
>
> returning:
> ['a', ':=', 'b', '+', 'c']
>
> whats the python way to achieve this, preferably without regexp?
I think regexps are likely the right way to do this kind of
tokenization.
The string split() method doesn't return the split value so that is
less than helpful for your application: 'a=b'.split() --> ['a',
'b']
The new str.partition() method will return the split value and is
suitable for successive applications: 'a:=b+c'.partition(':=') -->
('a', ':=', 'b+c')
FWIW, when someone actually does want something that behaves like
str.split() but with multiple split values, one approach is to replace
each of the possible splitters with a single splitter:
def multi_split(s, splitters):
first = splitters[0]
for splitter in splitters:
s = s.replace(splitter, first)
return s.split(first)
print multi_split( 'a:=b+c' , [':=','+'] )
Raymond
More information about the Python-list
mailing list