我想用numpy loadtxt將csv文件加載到數組中。但似乎我無法正確加載日期時間。Python numpy loadtxt失敗,日期時間
下面演示發生了什麼。我做錯什麼了嗎?
>>> s = StringIO("05/21/2007,03:27")
>>> np.loadtxt(s, delimiter=",", dtype={'names':('date','time'), 'formats':('datetime64[D]', 'datetime64[m]')})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/lib/npyio.py", line 573, in <lambda>
return lambda x: int(float(x))
ValueError: invalid literal for float(): 05/21/2007
[numpy.datetime64](http://docs.scipy.org/doc/numpy-dev/reference/arrays.datetime.html)需要一個破折號:'-',而不是斜線:'/'。不知道如何改變分隔符。 – askewchan 2013-05-01 19:02:21
但這個工程雖然: np.datetime64(「05/21/2007」) 2007-05-21 00:00:00 – 2013-05-01 19:12:29
請給你的代碼 – TN888 2013-05-01 19:19:34