2

I have a result list that contains something like

[(datetime.datetime(2013, 1, 1, 0, 1, 14),), (datetime.datetime(2013, 1, 1, 1, 33, 50),)]

How does one print it like 2013-01-01 00:01:14.000 , 2013-01-01 01:33:50.000 or a readable format.

I have tried printing them in string

import datetime
...
for date in results:
    print (str(date))

This prints

(datetime.datetime(2013, 1, 1, 0, 1, 14),)
(datetime.datetime(2013, 1, 1, 1, 33, 50),)
miradulo
  • 28,857
  • 6
  • 80
  • 93
Han
  • 131
  • 1
  • 16

3 Answers3

2

[(datetime.datetime(2013, 1, 1, 0, 1, 14),), (datetime.datetime(2013, 1, 1, 1, 33, 50),)] is a list where each element is a 1-element tuple.

You are printing the tuple rather than the datetime within at the first position, print(str(date[0])) to get the actual value.`

Uku Loskit
  • 40,868
  • 9
  • 92
  • 93
2

You need to extract the datetime object out of the tuple, for it to be printed in string format.

>>> for each in results:
...     print(each[0])
...
2013-01-01 00:01:14
2013-01-01 01:33:50
>>>
0

Just convert the tuple to string like:

str(datetime_object)