parsing a date

K

Kalle Anke

I want to parse a date string, for example '2005-09-23', and since I haven't
done this before I would like to ask what is the best way to do it.

I've looked around and the dateutil seems to be what most people use, but
unfortunately I only get an empty file when I try to download it. I also
tried the standard modules and ended up with this

import datetime
from time import strptime

d = '2005-09-23'
w = strptime(d,'%Y-%m-%d')
print datetime.date( w[0], w[1], w[2] )

But I suspect there is a better way to do it??
 
L

Larry Bates

Better than a single line of code? What is it that
you are looking for?

If you dates are consistent you can do:

year, month, day=map(int, d.split('-'))

but I'm not sure it is "better". I guess it depends
on what you want to do with them after parsing.

-Larry Bates
 
P

Peter

Kalle said:
I want to parse a date string, for example '2005-09-23', and since I haven't
done this before I would like to ask what is the best way to do it.

I've looked around and the dateutil seems to be what most people use, but
unfortunately I only get an empty file when I try to download it. I also
tried the standard modules and ended up with this

import datetime
from time import strptime

d = '2005-09-23'
w = strptime(d,'%Y-%m-%d')
print datetime.date( w[0], w[1], w[2] )
Well, thats not _to_ bad.
But I suspect there is a better way to do it??
There is...

I would check out "strftime"
With strftime you could do something like this...

# ------
print strftime("%Y-%m-%d")
# -----

or if you wanted to get the time in the past or future you could do
something like

# ------
tm = time.localtime(time.time() + ((60 * 60) * 24))
print strftime("%Y-%m-%d", tm)
# -----

Which gets the date of tommarow.

HTH,
Peter
 
K

Kalle Anke

but I'm not sure it is "better". I guess it depends
on what you want to do with them after parsing.


Sorry, I should have been clearer. I want to parse the date and create a
'date object' that is a part of larger object (I'm parsing a text file that
represents the larger object and the date is a part of it).

So my question should probably be: is there a better way to parse the date
and generate the 'date object' than the two step

w = strptime(d,'%Y-%m-%d')
datetime.date( w[0], w[1], w[2] )

Since I'm new to many things in Python I'm trying to learn if there is a
"better" way of doing things.

jem
 
P

Peter Hansen

Kalle said:
but I'm not sure it is "better". I guess it depends
on what you want to do with them after parsing.

Sorry, I should have been clearer. I want to parse the date and create a
'date object' that is a part of larger object (I'm parsing a text file that
represents the larger object and the date is a part of it).

So my question should probably be: is there a better way to parse the date
and generate the 'date object' than the two step

w = strptime(d,'%Y-%m-%d')
datetime.date( w[0], w[1], w[2] )

Since I'm new to many things in Python I'm trying to learn if there is a
"better" way of doing things.

You're still not defining what "better" means to you, so who can say?

Perhaps you think a single line would be "better"?

datetime.date(*time.strptime(d, '%Y-%m-%d')[:3])

-Peter
 
K

Kalle Anke

Kalle said:
but I'm not sure it is "better". I guess it depends
on what you want to do with them after parsing.

Sorry, I should have been clearer. I want to parse the date and create a
'date object' that is a part of larger object (I'm parsing a text file that
represents the larger object and the date is a part of it).

So my question should probably be: is there a better way to parse the date
and generate the 'date object' than the two step

w = strptime(d,'%Y-%m-%d')
datetime.date( w[0], w[1], w[2] )

Since I'm new to many things in Python I'm trying to learn if there is a
"better" way of doing things.

You're still not defining what "better" means to you, so who can say?

Perhaps you think a single line would be "better"?

datetime.date(*time.strptime(d, '%Y-%m-%d')[:3])


:) It's not my day I think

Better (in this case) =

+ Being "pythonic"

+ Avoiding going through a second representation (the tuple)
if there is some way to create a date object directly.

jem
 
P

Peter Hansen

Kalle said:
Better (in this case) =

+ Being "pythonic"

+ Avoiding going through a second representation (the tuple)
if there is some way to create a date object directly.

I think the plainest and simplest approach would be to create a
well-named function which does the necessary steps (_not_ in a single
line of code but with nice readable temporary variables) and call that
whenever you need it.

Simple utility functions that call a few standard library functions to
do a job like this are very "pythonic", and trying to avoid a second
representation when (as in this case) there's no obvious way to do it is
not pythonic. :)

I'd say if you've checked datetime.date() and not found a factory
function or whatever which knows how to parse a string (as many of us
have), and if you don't want to download, install, and depend on a
third-party extension such as mxDateTime which do this already, then a
function to wrap the ugly details is a very nice way to proceed.

If you need to do this often, then you've got the beginnings of your own
utility module or package, and having those around is pretty "pythonic" too.

(An alternative would be to contribute a patch to add something like
this to the datetime module. I don't know if it would be accepted, but
at least it would be the true arbiters of "pythonicism" who would be
judging the matter, not you or I. :) )

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top