使用genfromttxt读取csv/txt文件,同时分隔数字和日期时间的值



我拼命地想读取并转换一个如下的txt文件:

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

我已经能够通过使用将所有数值读取为变量

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)

但是,"date_time"列(她的col 1)没有显示为单独的变量,也没有返回任何错误消息

我想将事物转换为以下变量:

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>

我已经找到了几个使用、genfromttxt、dtype和strptime的例子,但我没有一个适用于这个特定的案例。

对新手有什么建议吗?

____________________________________________

编辑(24-07-2013):

我找到了一个足够好的解决方案:

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)


返回:

[(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')]


现在,也许有人可以帮我找到:

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

其中DT1包括:

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


感谢您的帮助

您的方法已经非常好了,它提供了一个结构化数组,您可以使用operator.itemgetter:从中获取字段

names = ('Line', 'Date', 'Temperature', 'Humidity','DewPoint')
from operator import itemgetter
n0, DT1, T2, H3, DP4 = itemgetter(*names)(data1)

相关内容

  • 没有找到相关文章

最新更新