question on storing dates in a tuple
Chris Rebert
clp2 at rebertia.com
Wed Feb 10 20:30:52 EST 2010
On Wed, Feb 10, 2010 at 5:17 PM, m_ahlenius <ahleniusm at gmail.com> wrote:
> Hi,
>
> I have a weird question about tuples. I am getting some dates from a
> mysql db, using the mysqldb interface. I am doing a standard query
> with several of the fields are "datetime" format in mysql.
> When I retrieve and print them in python, they look fine.
>
> eg.
> storeddate = 2010-02-07 12:03:41
>
> But when I append these dates into a string to write to an ascii log
> file, they come out as:
>
> datetime.datetime(2010, 2, 7, 12, 03, 41) (or something along those
> lines).
>
> It appears that I am dealing with some sort of date object here. So
> I am just trying to understand what's going on, and whats the best
> work with such objects.
http://docs.python.org/library/datetime.html#datetime.datetime
>>> from datetime import datetime
>>> now = datetime.now()
>>> print str(now)
2010-02-10 17:22:12.459277
>>> print repr(now)
datetime.datetime(2010, 2, 10, 17, 22, 12, 459277)
>>> print (now,)
(datetime.datetime(2010, 2, 10, 17, 22, 12, 459277),)
tuples use repr() on their elements when they're converted to strings,
and repr() on datetime-s uses the constructor notation as opposed to
the more normal notation used by str(). Same sort of thing happens
with strings; note the presence/lack of quotes:
>>> string = "aa"
>>> print str(string)
aa
>>> print repr(string)
'aa'
>>> print (string,)
('aa',)
Cheers,
Chris
--
http://blog.rebertia.com
More information about the Python-list
mailing list