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

Ek Esawi esawiek at gmail.com
Tue May 17 13:11:45 EDT 2016


Hi All—



I am reading data from a file using genfromtxt. A part of my data (input,
output, and desired output) is shown below which consists of string,
integer, time, and date type data. I want to read the data so that the
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).



I tried to use structured dtype but without success and I don’t want to use
pandas. What is the best remedy to achieve the desired output shown below?



Thanks in advance—EK



++++++++++++++++++++++++++++++



Code



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

MyFile=New_file



CRNs={Date: CF,Time:CF1,In:CF1,Dur:CF1,PP:CF1,Pre:CF1}
                               #converters



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

358237   5/25/1993    New             12:22   1:31        4:16
12:03      160       12:13



Output



[ (312171, 1995-07-01, bOld, 13:37:00, 01:43:00, 04:42:00, 13:16:00, 162,
13:19:00)

 (358237, 1993-05-25, bNew, 12:22:00, 01:31:00, 04:16:00, 12:03:00, 160,
12:13:00)]



Desired output



[ (312171, 1995-07-01, bOld, 13:37:00, 01:43:00, 04:42:00, 13:16:00, 162,
13:19:00)

 (358237, 1993-05-25, bNew, 12:22:00, 01:31:00, 04:16:00, 12:03:00, 160,
12:13:00)]


More information about the Tutor mailing list