我拼命地尝试读取和转换这样的 txt 文件:
文件.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>
我找到了几个使用 genfromtxt、dtype 和 strptime 的示例,但我没有一个可以用于这种特定情况。
对新手有什么建议吗?
_ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __
_ __ _ __ _ __ _ _ _ __ _ __ _ __ _ __ _ __ _ _ __ _ __ _
编辑(2013 年 7 月 24 日):
我找到了一个可能的解决方案,足以满足我的需求:
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)
谢谢你的帮助