【发布时间】:2018-12-07 03:41:29
【问题描述】:
当我使用以下 np.loadtxt 代码加载格式的数据时:
2017-07-26,153.3500,153.9300,153.0600,153.5000,153.5000,12778195.00
数据加载得很好,加载txt代码->
a, b, c, d, e, f, g = np.loadtxt("goog.csv",
dtype={'names': ("b'Date", 'Open', 'High', 'Low', 'Close', 'Adjusted_close', 'Volume'),
'formats': ('U10', np.float, np.float, np.float, np.float, np.float, np.float)},
delimiter=',',
skiprows=1,
unpack=True)
print(a)
输出->
['2017-07-26' '2017-07-25' '2017-07-24' ..., '2000-01-05' '2000-01-04'
'2000-01-03']
Process finished with exit code 0
但是在使用相应的 np.genfromtxt 代码时会出现 ValueError: too many values to unpack,我使用了以下 genfromtxt 代码->
a, b, c, d, e, f, g = np.genfromtxt('goog.csv',
dtype={'names': ("b'Date", 'Open', 'High', 'Low', 'Close', 'Adjusted_close', 'Volume'),
'formats': ('U10', np.float, np.float, np.float, np.float, np.float, np.float)},
delimiter=',',
skip_header=1,
unpack=True)
print(a)
输出->
Traceback (most recent call last):
File "C:/Users/sonika jha/PycharmProjects/csvCheck/csvCheck.py", line 84, in <module>
download_stock_data()
File "C:/Users/sonika jha/PycharmProjects/csvCheck/csvCheck.py", line 66, in download_stock_data
unpack=True)
ValueError: too many values to unpack (expected 7)
Process finished with exit code 1
我的最终目标是使用 genfromtxt 以字符串数据类型加载日期,其余的以浮点数加载。
【问题讨论】:
-
两种情况下的输出是什么?错误是什么?
-
@sophros 进行了编辑,请查看。
标签: python-3.x numpy genfromtxt