String Problem, What is it I don't understand?

Terry Hancock hancock at anansispaceworks.com
Fri Jan 17 05:44:58 EST 2003


Peter Hansen wrote:
> Burkhard posted the one answer, so I'm just going to point out
> that a much simpler approach for this particular problem is
> to use "split", as in
> 
>  month, day, year = xxx.split('/')
> 
> Unless you have to deal with dates in which the separator
> can be one of a number of options (ugh) this is much more
> readable, probably faster (not that it matters), and
> would handle four-digit years quite nicely as well.

Actually the alternate separator case is not difficult either:

import re
xxx = '1/15-2003'
month, day, year = re.split(r'[/-]', xxx)

Note also that 4 or 1 digit entries don't cause me any trouble, either.  

Cheers,
Terry

-- 
Anansi Spaceworks
http://www.anansispaceworks.com




More information about the Python-list mailing list