2015-02-07 19 views
1

我有一個看起來像許多文本文件:導入多個txt文件與column0 = datetime對象和COLUMN1 numpy的陣列= float值

#comment 
2012-01-01 00:00:00, 6542736.60466 
2012-01-01 00:00:05, 6542736.60466 
2012-01-01 00:00:10, 6568774.53588 
2012-01-01 00:00:15, 6594812.46709 
... 
2012-01-01 23:59:55, 6494801.44322 

有每一天的文本文件,所以最終我想堆疊在文本文件中的數據在一個數組,使得次繼續無縫地(例如,從23點59分55秒2012-01-01 2012-01-02到00:00:00)

monitor1="list of file names in a directory" 

for x in monitor1: 
    x=np.genfromtxt((filepath+"\\"+x),comments='#',delimiter=',') 
monitor1array=np.vstack(monitor1) 
for x in monitor1array[:,0]: 
    x=datetime.datetime.strptime(x, "%Y-%m-%d %H:%M:%S") 

這代碼似乎不起作用。 1)它似乎無法在第一個for循環中創建數組列表。 2)它不承認datetime作爲列0的類型。

請幫助!

回答

1

我認爲你將有一個更好的經驗reading csvs with pandas

In [11]: import pandas as pd 

In [12]: pd.read_csv('foo.csv', header=None, comment='#') 
Out[12]: 
        0    1 
0 2012-01-01 00:00:00 6542736.60466 
1 2012-01-01 00:00:05 6542736.60466 
2 2012-01-01 00:00:10 6568774.53588 
3 2012-01-01 00:00:15 6594812.46709 

的日期應該正確解析,如果不是在列中使用pd.to_datetime

In [13]: df[0] = pd.to_datetime(df[0]) 
+0

我會怎麼寫這在for循環for monitor1中的所有文件?有很多文件,所以我不能做典型的df1 = pd.read_csv(filename,header = None,comment ='#') – Solar 2015-02-07 05:50:08

+0

@Solar你可以將它們保存爲一個列表(例如理解),然後'pd.concat'他們。 – 2015-02-07 06:32:59