By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,169 Members | 1,705 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,169 IT Pros & Developers. It's quick & easy.

parsing a date

P: n/a
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??

Sep 23 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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

Kalle Anke wrote:
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??

Sep 23 '05 #2

P: n/a
Kalle Anke wrote:
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
Sep 23 '05 #3

P: n/a
On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote
(in article <ht********************@comcast.com>):
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

Sep 24 '05 #4

P: n/a
Kalle Anke wrote:
On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
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
Sep 24 '05 #5

P: n/a
On Sat, 24 Sep 2005 16:06:06 +0200, Peter Hansen wrote
(in article <04******************************@powergate.ca>) :
Kalle Anke wrote:
On Fri, 23 Sep 2005 23:01:18 +0200, Larry Bates wrote:
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

Sep 24 '05 #6

P: n/a
Kalle Anke wrote:
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
Sep 24 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.