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

Alan Gauld alan.gauld at yahoo.co.uk
Wed May 18 02:44:49 EDT 2016


On 17/05/16 18:11, Ek Esawi wrote:

> output comes out in the correct format (the desired output as shown below).
> I used converters to covert time and date values, but all came out in
> string format (output below).

What makes you think they are strings? I would expect to see quote signs
if they were strings but there are no quotes...

> CF = lambda date: datetime.strptime(bytes.decode(date),
> %m/%d/%Y).strftime(%Y-%m-%d)  # convert data
> 
> CF1 = lambda time: datetime.strptime(bytes.decode(time),
> %H:%M).strftime(%H:%M:%S)       # convert time

But both of these lambdas return strings (strftime)
You parse the date/time from the bytes then call strftime
on the result which returns a string.

But also you don't have quotes around your format strings
so you should get a syntax error. Please post the actual
code you are using.

> 
> MyFile=New_file
> CRNs={Date: CF,Time:CF1,In:CF1,Dur:CF1,PP:CF1,Pre:CF1}
> data = np.genfromtxt(MyFile,
>            names=True,delimiter=,,converters=CRNs,dtype=None)
> 
> Input
> O            Date            Name           Time    Int
> Dur           PP           H          Pred
> 
> 312171   7/1/1995      Old              13:37   1:43        4:42
> 13:16      162       13:19
> 
> Output
> [ (312171, 1995-07-01, bOld, 13:37:00, 01:43:00, 04:42:00, 13:16:00, 162,
> 13:19:00)
> 
> 
> Desired output
> [ (312171, 1995-07-01, bOld, 13:37:00, 01:43:00, 04:42:00, 13:16:00, 162,
> 13:19:00)

Your desired output looks exactly like the real output so it's
not clear what exactly you want to be different?


-- 
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