0

I am desperately trying to read and convert a txt file like this:

file.txt


Line,Date Time,Celsius(°C),Humidity(%rh),Dew Point(°C),Serial Number
1,10-06-2013 18:25:00,24.0,48.5,12.5,990121703
2,10-06-2013 18:30:00,24.0,48.0,12.3
3,10-06-2013 18:35:00,23.5,48.5,12.0
4,10-06-2013 18:40:00,23.5,49.0,12.2
5,10-06-2013 18:45:00,23.5,49.0,12.2
6,10-06-2013 18:50:00,23.5,49.0,12.2
7,10-06-2013 18:55:00,23.5,49.0,12.2
...

I have been able to read all numeric values into variables by using:

from pylab import *
from datetime import datetime
fname ='LOG.txt'
n0,DT1,T2,H3,DP4 = genfromtxt(fname,delimiter=',', skip_header=1, skip_footer=0,usecols=(0,1,2,3,4), autostrip=True, unpack=True, invalid_raise=True)

However, the 'date_time' column (her col 1) is not shown as a separate variable nor is there any error message returned.

I would like is to convert things into the to following variables:

n0   = 0 column as 'u4'<br>
DT1  = 1st column converted such that `datetime.strptime('10-06-2013 18:25:01', '%d-%m-%Y %H:%M:%S')`<br>
T2   = 2nd column as 'f4'<br>
H3   = 3rd column as 'f4'<br>
DP4  = 4th column as 'f4'<br>

I have found several examples using, genfromtxt, dtype, and strptime but I got none of them to work for this specific case.

Any suggestions for a newbie?

____________________________________
____________________________________

EDIT (24-07-2013):

I found a possible solution good enough for what I would like:

from pylab import *
import datetime as DT

def make_date(datestr):
    return DT.datetime.strptime(datestr, '%d-%m-%Y %H:%M:%S')

data1 = genfromtxt(fname, delimiter = ',',
                skip_header=1,skip_footer=0,usecols = (0,1,2,3,4), # usecols (0..4) is required due to the serial number present in second row only 
                converters = {'Date':make_date},
                names =  ('Line', 'Date', 'Temperature', 'Humidity','DewPoint'),
                dtype = None,
                invalid_raise=True) # dtype = None takes care of all data type but the one sent to converters

# Console output:
print(data1)
print(data1.dtype)

#Temperature Graph:
figure(1)
plot(data1['Date'],data1['Temperature'],'-xb')
grid('on')
ylabel('Temperature (degC)',fontsize=10)
xlabel('Date',fontsize=10)


This returns:

[(1, datetime.datetime(2013, 6, 10, 18, 25), 24.0, 48.5, 12.5)
(2, datetime.datetime(2013, 6, 10, 18, 30), 24.0, 48.0, 12.3)
(3, datetime.datetime(2013, 6, 10, 18, 35), 23.5, 48.5, 12.0) ...,
(12298, datetime.datetime(2013, 7, 23, 11, 10), 23.5, 43.5, 10.4)
(12299, datetime.datetime(2013, 7, 23, 11, 15), 23.5, 43.5, 10.4)
(12300, datetime.datetime(2013, 7, 23, 11, 20), 23.5, 43.5, 10.4)]
[('Line', '<i4'), ('Date', 'O'), ('Temperature', '<f8'), ('Humidity', '<f8'), ('DewPoint', '<f8')]


Now, maybe someone can help me to get to:

n0,DT1,T2,H3,DP4 = genfromtxt(fname,...,unpack=True)

where DT1 consists of:

datetime.datetime(2013, 6, 10, 18, 25)


Thanks for any help

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
Pythonio
  • 2,039
  • 2
  • 12
  • 5

1 Answers1

0

Your approach is already very good, it gives a structured array from where you can get the fields using operator.itemgetter:

names = ('Line', 'Date', 'Temperature', 'Humidity','DewPoint')

from operator import itemgetter
n0, DT1, T2, H3, DP4 = itemgetter(*names)(data1)
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234