Various strings to dates.

Michael Spencer michael at telcopartners.com
Sat Jan 24 15:53:27 EST 2004


[Fixed top-posts: please add future comments at the bottom]
> >
> > >
> > > Amy G wrote:
> > > > I have seen something about this beofore on this forum, but my
google
> > search
> > > > didn't come up with the answer I am looking for.
> > > >
> > > > I have a list of tuples.  Each tuple is in the following format:
> > > >
> > > > ("data", "moredata", "evenmoredata", "date string")
> > > >
> > > > The date string is my concern.  This is the date stamp from an
email.
> > > > The problem is that I have a whole bunch of variations when it comes
> to
> > the
> > > > format that the date string is in.  For example I could have the
> > following
> > > > two tuples:
> > > >
> > > > ("data", "moredata", "evenmoredata", "Fri, 23 Jan 2004 00:06:15")
> > > > ("data", "moredata", "evenmoredata", "Thursday, 22 January 2004
> > 03:15:06")
> > > >
> > > > I know there is some way to use the date string from each of these
to
> > get a
> > > > date usable by python, but I cannot figure it out.
> > > > I was trying to use time.strptime but have been unsuccesful thus
far.
> > > >
> > > > Any help is appreciated.
> > > >
> > "wes weston" <wweston at att.net> wrote in message
> > news:MFgQb.95539$6y6.1915432 at bgtnsc05-news.ops.worldnet.att.net...
> > > Amy,
> > >     I hope there is a better way but, if you go here:
> > >
> > > http://www.python.org/doc/current/lib/datetime-date.html
> > >
> > > The new datetime module may help. This and the time mod
> > > should get you where you want to go.
> > >
> > > list     = strdate.split(", ")
> > > daystr   = list[0]
> > > daynum   = int(list[1])
> > > monthstr = list[2]
> > > year     = int(list[3])
> > > #funct to get a month int is needed
> > >
> > > d = datetime.Date(y,m,d)
> > >
> > > wes
> > >
> > > ---------------------------------------
> > > >
> > >
> "Amy G" <amy-g-art at cox.net> wrote in message
> news:PRgQb.16209$AA6.9881 at fed1read03...
> > No it won't.  Unfortunatly I don't necessarily have a comma delimited
date
> > string.  Thanks for the input though.
> >
> > The following three date strings is another example of the various date
> > formats I will encounter here.
> >
> > Thursday, 22 January 2004 03:15:06
> > Thursday, January 22, 2004, 03:15:06
> > 2004, Thursday, 22 January 03:15:06
> >
> > All of these are essentially the same date... just in various formats.
I
> > would like to parse through them and get a comparable format so that I
can
> > display them in chronological order.
> >
> >
>
>
"Amy G" <amy-g-art at cox.net> wrote in message
news:n7iQb.16224$AA6.5862 at fed1read03...
> Okay.  I fixed the problem somewhat.  I moved the dateutil directory over
to
> /usr/local/lib/python2.2/site-packages and I can now import dateutil.  But
a
> call like this:
>
> from dateutil.parser import parse
>
> results in this error:
>
> ImportError: cannot import name parse
>
> I can 'from dateutil.parser import *' but cannot use parse after that.
> I can also 'from dateutil import parser' but that doesn't help either.
>
> Sorry for my inexperience here.  Thanks for all of the help already.
>

Amy:

The docstring of the dateutil package:
"""
Copyright (c) 2003  Gustavo Niemeyer <niemeyer at conectiva.com>

This module offers extensions to the standard python 2.3+
datetime module.
"""
__author__ = "Gustavo Niemeyer <niemeyer at conectiva.com>"
__license__ = "PSF License"

notes that it requires the datetime module (new in Python 2.3).  It appears
that you are using 2.2.  Can you install 2.3.3?

If you need to stick with 2.2, the question of using datetime in 2.2 was
answered in
http://groups.google.com/groups?q=python+2.2+datetime&hl=en&lr=&ie=UTF-8&selm=mailman.1043334342.25146.python-list%40python.org&rnum=2
with a pointer to a "workalike" module, see:
http://cvs.zope.org/Zope3/src/datetime/

I have not tested whether this works with dateutil

Cheers

Michael






More information about the Python-list mailing list