使用Numpy读取CSV文件

时间:2014-05-27 21:59:30

标签: python csv numpy

from numpy import genfromtxt
dataPoints = genfromtxt(temp.csv, dtype='datetime64[D],i8',delimiter=' ')

CSV文件:temp.csv

2014-05-19 10
2014-05-20 11
2014-05-21 12

输出 print(dataPoints)

[ nan  10.]
[ nan  11.]
[ nan  12.]

修改

[1969-12-31 00:00:00 1970-01-11 00:00:00]
[1969-12-31 00:00:00 1970-01-12 00:00:00]
[1969-12-31 00:00:00 1970-01-13 00:00:00]
[1969-12-31 00:00:00 1970-01-14 00:00:00]
[1969-12-31 00:00:00 1970-01-13 00:00:00]

1 个答案:

答案 0 :(得分:4)

告诉NumPy期待什么类型。 E.g:

>>> genfromtxt('temp.csv', dtype='datetime64[D],i8')
array([(datetime.date(2014, 5, 19), 10L),
       (datetime.date(2014, 5, 20), 11L), (datetime.date(2014, 5, 21), 12L)], 
      dtype=[('f0', '<M8[D]'), ('f1', '<i8')])