[Tutor] String Tokenizer - As in Java

Rick Pasotto rick at niof.net
Tue Aug 19 11:41:26 EDT 2003


On Tue, Aug 19, 2003 at 09:56:19AM -0400, Marc Barry wrote:
> Thanks for the help.
> 
> I tried to google for it also and I didn't find those two links.  The 
> second one gave me the answer I was looking for as it seems that 'split' 
> does exactly what I want.
> 
> Although, my interpretation is that 'split' only only allows the defintion 
> of one separator.  This is okay for most things, but I have strings that I 
> would like to split with two separators such as '.' and '@'.  I don't think 
> that I can use split to handle this and therefore will have to resort to 
> something more powerful (i.e. regular expressions).

Two separators means using split twice.

s = s'tring.with.two.seps at in.it'
l = [x.split('.') for x in s.split('@')]

gives:

[['string', 'with', 'two', 'seps'], ['in', 'it']]

Now you've got a list of lists which can be combined:

z = []
for x in l: z += x

which gives:

['string', 'with', 'two', 'seps', 'in', 'it']

Does that do what you want? BTW, there's probably a better way to do the
last step (or to combine them) but I can't come up with it at the moment.

-- 
"Nothing is so aggravating as calmness." -- Oscar Wilde
    Rick Pasotto    rick at niof.net    http://www.niof.net



More information about the Tutor mailing list