[Tutor] genfromtxt and dtype to convert data to correct format

Alan Gauld alan.gauld at yahoo.co.uk
Thu May 19 04:37:57 EDT 2016


On 19/05/16 01:31, Ek Esawi wrote:
> Thanks Alan!
> 
> Taking the strtime off did not work either and printed dattime.dattime(very
> long date format). 

But isn't that just the representation of a datetime object (which is
what strptime produces)? In other words it's an object not a string.

Can you show us what you get back?
Or try printing the type() of the object:
eg.
print (type( row[1])
print (type( row[3])

You may be getting confused between the representation of the
object and the actual data. But until we see some actual code
and the actual output it produces it is hard to be sure.

Can you send us a cut 'n paste of an actual python session
so that we can see the parameters to genfromtxt and how
you are producing the output?

For example I'd expect something like:

>>> import datetime as dt
>>> d = dt.datetime.strptime("07/05/2015","%m/%d/%Y")
>>> t = (1,d,'foo')
>>> print(t)
(1, datetime.datetime(2015, 7, 5, 0, 0), 'foo')
>>>

Notice the middle entry is a datetime object, which is,
I think, what you want? You can get just the date (or time)
portion if you want by calling the appropriate method.
You could do that in your lambdas:

>>> CF = lambda datestr: dt.datetime.strptime(datestr,
                                              '%m/%d/%Y').date()
>>> d2 = CF('11/08/2012')
>>> data = (1,d2,'My string')
>>> print(data)
(1, datetime.date(2012, 11, 8), 'My string')
>>>

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list