beginners question about return value of re.split

klaus kla at us.de
Mon Mar 24 16:11:45 EDT 2008


On Fri, 21 Mar 2008 13:34:27 -0700, John Machin wrote:

> On Mar 22, 2:53 am, klaus <k... at us.de> wrote:
>> On Fri, 21 Mar 2008 10:31:20 -0500, Tim Chase wrote:
>>
>> <..........>
>>
>> Ok thank you !
>>
>> I think I got a bit lost in all the possibilities python has to offer.
> 
> IMHO you got more than a bit lost. You seem to have stumbled on a
> possibly unintended side effect of re.split.
> 
> What is your underlying goal?
> 
> If you want merely to split on '-', use datum.split('-').
> 
> If you want to verify the split results as matching patterns (4 digits,
> 2 digits, 2 digits), use something like this:
> 
> | >>> import re
> | >>> datum = '2008-03-14'
> | >>> pattern = r'^(\d\d\d\d)-(\d\d)-(\d\d)\Z' You may notice two
> differences between my pattern and yours ... 
> | >>> mobj = re.match(pattern, datum) | >>> mobj.groups()
> | ('2008', '03', '14')
> 
> But what are you going to do with the result? If the resemblance between
> '2008-03-14' and a date is not accidental, you may wish to consider
> going straight from a string to a datetime or date object, e.g.
> 
> | >>> import datetime
> | >>> dt = datetime.datetime.strptime(datum, '%Y-%m-%d') 
> | >>> dt
> | datetime.datetime(2008, 3, 14, 0, 0) 
> | >>> d =
>  datetime.datetime.date(dt)
> | >>> d
> | datetime.date(2008, 3, 14)
> 
> HTH,
> John

Ok, sorry for my late reply. I got caught up in a fight with easterbunnys
over some extraordinary large, fruitty and fertile eggs. Some creatures 
take Easter just to serious and it is not even mating season ! Can you 
believe that ?

:-)

Anyway, the underlying goal was to verify user input and to split up the 
date so that I could easily convert it to another format. Among others, 
an url and for a database querry. And I have succeeded in that.

Thank you again; for taking the time to explain - and to question.

KL.



More information about the Python-list mailing list