我试图在iPython笔记本中加载一个存在于桌面上的.csv文件,但它显示错误为无效语法
这是我使用的代码和文件:
data = np.loadtxt("C:/Users/rj/Desktop/data.csv",dtype={ 'formats':('S10', 'f8','f8','f8','f8', 'f8','f8','f8','f8')},delimiter=',')
data.csv文件包含:
24-Dec-15,378.45,380.9,384.75,377.6,382.35,382.4,382.39,4568751
28-Dec-15,382.4,384.9,395,383.75,394.85,394,391.54,7166351
29-Dec-15,394,392.9,397.5,388.75,390.7,391.85,392.95,7359611
30-Dec-15,391.85,392,395,390.5,394,393.45,393.11,4866177
31-Dec-15,393.45,394,395.75,389.15,391.6,391.3,391.85,6410622
01-Jan-16,391.3,392.5,403,373,401.8,401.9,398.24,4377363
04-Jan-16,401.9,400,400.1,375.05,376.15,377.05,383.74,7822660
05-Jan-16,377.05,381.05,382.45,372.1,373,374.45,377.36,6901068
06-Jan-16,374.45,374.25,375.5,364.6,365,365.9,370.04,7211230
07-Jan-16,365.9,356.25,358,338.1,344.8,343.55,347.83,11782307
08-Jan-16,343.55,345.6,355.85,345.6,353.9,353.35,351.97,8770370
错误是:
File "<ipython-input-13-177939f245ba>", line 21
... 'formats':('S10', 'f8','f8','f8','f8', 'f8','f8','f8','f8')},delimiter=',')
^
SyntaxError: invalid syntax
如何纠正语法?
答案 0 :(得分:0)
怎么样:
import numpy as np
names = ['date', 'a', 'b', 'c', 'e', 'f', 'g', 'h', 'i']
formats = ['S10', 'f8','f8','f8','f8', 'f8','f8','f8','f8']
data = np.loadtxt('C:/Users/rj/Desktop/data.csv', \
dtype=list(zip(names, formats)), delimiter=',')
当然,您可能更喜欢更有意义的names
。 Python 2在list(...)
对象上不需要zip
。