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

Ek Esawi esawiek at gmail.com
Thu May 19 07:51:02 EDT 2016


Thanks again!

I tried a combination of the suggestions and still not getting what i want.

Here are the original code, file, and output.

CODE

mport csv; import numpy as np; from datetime import datetime, date, time

CF = lambda date: datetime.strptime(bytes.decode(date),
'%m/%d/%Y').strftime('%Y-%m-%d')
CF1 = lambda time: datetime.strptime(bytes.decode(time),
'%H:%M').strftime('%H:%M:%S')
MyFile='c:/Users/EK Esawi/My Documents/Temp/GOSA-3.csv'
CRNs={'Date':
CF,'Time':CF1,'Interval':CF1,'Duration':CF1,'Preplay':CF1,'Prediction':CF1}

data = np.genfromtxt(MyFile,
names=True,delimiter=',',converters=CRNs,dtype=None)
print(data)

INPUT

O Date Geyser Time Interval Duration Preplay Heght Prediction 312171
7/1/1995 Position 13:37 1:43 4:42 13:16 162 13:19 358237 5/25/1993 Position
12:22 1:31 4:16 12:03 160 12:13 339971 7/17/1994 Position 15:54 1:23 4:36
15:43 160 15:51

OUTPUT

[ (312171, '1995-07-01', b'Old Faithful', '13:37:00', '01:43:00',
'04:42:00', '13:16:00', 162, '13:19:00')
 (358237, '1993-05-25', b'Old Faithful', '12:22:00', '01:31:00',
'04:16:00', '12:03:00', 160, '12:13:00')
 (339971, '1994-07-17', b'Old Faithful', '15:54:00', '01:23:00',
'04:36:00', '15:43:00', 160, '15:51:00')


More information about the Tutor mailing list