3

我试图让 Python 用 500 只股票的收盘价填充列表。虽然代码似乎只适用于少数股票,但数量过多会带来问题。Python 不断给我以下错误:“ OneClose.append(Data[i][4]) IndexError: list index out of range.”我不知道如何解决这个问题。无论我似乎在范围字段中输入了什么(在本例中为 31),错误仍然存​​在。

raw = open('C:\Model\Stocks\list.txt', 'r').read()
stocks = raw.split('\n')

for stock in stocks:
    Data = ystockquote.get_historical_prices(stock, '20120501', '20120716')

# Create empty lists, quick and dirty
    OneClose = [ ]
    OneDate = [ ]

# Populate lists from downloaded data
    for i in range(1, 31):
        OneDate.append(Data[i][0])
        OneClose.append(Data[i][4])

Data包含:

Data = [['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos'], 
        ['2012-07-13', '86.32', '87.83', '86.32', '87.54', '1937700', '87.5'], 
        ['2012-07-13', '86.32', '87.83', '86.32', '87.59', '2599300', '87.5'], 
        ['2012-07-12', '86.60', '86.97', '85.34', '86.41', '4938300', '86.4'], 
        ['2012-07-11', '88.38', '88.76', '87.01', '87.85', '4208600', '87.8'], 
        ['2012-07-10', '89.08', '89.82', '87.96', '88.25', '3356800', '88.2'], 
        ['2012-07-09', '88.86', '89.08', '88.31', '88.96', '2600900', '88.9'], 
        ['2012-07-06', '88.81', '89.38', '88.35', '88.99', '2192900', '88.9'], 
        ['2012-07-05', '89.20', '89.95', '88.86', '89.57', '1924800', '89.5'], 
        ['2012-07-03', '89.11', '89.75', '89.08', '89.70', '1410400', '89.7'], 
        ['2012-07-02', '89.29', '89.69', '88.28', '89.28', '2289700', '89.2'], 
        ['2012-06-29', '88.62', '89.60', '88.35', '89.60', '4629400', '89.6'], 
        ['2012-06-28', '86.36', '87.25', '85.98', '87.16', '3267100', '87.1'], 
        ['2012-06-27', '86.39', '87.60', '86.24', '87.16', '2887800', '87.1'], 
        ['2012-06-26', '85.91', '86.47', '85.29', '86.18', '2389100', '86.1'], 
        ['2012-06-25', '85.87', '86.31', '85.46', '85.85', '2908900', '85.8'], 
        ['2012-06-22', '87.11', '87.14', '86.38', '86.83', '3026000', '86.8'], 
        ['2012-06-21', '88.04', '88.38', '86.62', '86.73', '3722800', '86.7'], 
        ['2012-06-20', '87.93', '88.28', '87.35', '87.54', '3446800', '87.5'], 
        ['2012-06-19', '87.82', '88.17', '87.52', '87.82', '3212500', '87.8'], 
        ['2012-06-18', '87.07', '87.61', '86.67', '87.31', '2950600', '87.3'], 
        ['2012-06-15', '87.57', '87.91', '87.01', '87.44', '4696100', '87.4'], 
        ['2012-06-14', '86.31', '87.23', '86.06', '86.86', '3334900', '86.8'], 
        ['2012-06-13', '86.30', '86.80', '85.72', '86.13', '3370500', '86.1'], 
        ['2012-06-12', '85.67', '86.74', '85.22', '86.73', '2570900', '86.7'], 
        ['2012-06-11', '86.54', '86.54', '85.08', '85.18', '2098200', '85.1'], 
        ['2012-06-08', '85.18', '86.09', '84.99', '86.00', '2146200', '86.0'], 
        ['2012-06-07', '85.59', '86.19', '85.33', '85.50', '3032400', '85.5'], 
        ['2012-06-06', '83.22', '84.64', '83.03', '84.63', '2776300', '84.6'], 
        ['2012-06-05', '82.59', '82.86', '82.25', '82.51', '3268700', '82.5'], 
        ['2012-06-04', '82.79', '82.99', '81.99', '82.62', '2899900', '82.6'], 
        ['2012-06-01', '83.05', '83.98', '82.72', '82.85', '3539200', '82.8'], 
        ['2012-05-31', '84.45', '85.05', '83.87', '84.41', '3683200', '84.4'], 
        ['2012-05-30', '85.06', '85.12', '84.10', '84.45', '2697100', '84.4'], 
        ['2012-05-29', '85.38', '86.00', '85.13', '85.73', '2142200', '85.7'], 
        ['2012-05-25', '84.87', '85.20', '84.40', '84.78', '2346000', '84.7'], 
        ['2012-05-24', '84.96', '85.00', '84.03', '84.98', '2322100', '84.9'], 
        ['2012-05-23', '83.37', '84.88', '82.95', '84.80', '3200000', '84.8'], 
        ['2012-05-22', '84.69', '84.99', '83.68', '84.13', '3000900', '84.1'],
        ['2012-05-21', '83.88', '84.63', '83.75', '84.46', '2450000', '84.4'],
        ['2012-05-18', '84.78', '84.78', '83.27', '83.51', '3602900', '83.5'],
        ['2012-05-17', '85.56', '85.64', '84.25', '84.34', '2777200', '84.3'],
        ['2012-05-16', '85.91', '86.27', '85.26', '85.35', '2770600', '85.3'],
        ['2012-05-15', '85.95', '86.46', '85.50', '85.78', '2611300', '85.1'],
        ['2012-05-14', '85.46', '86.55', '85.24', '85.83', '2631900', '85.2'],
        ['2012-05-11', '86.47', '87.48', '86.24', '86.73', '2601800', '86.1'],
        ['2012-05-10', '87.14', '87.69', '86.88', '87.13', '2828500', '86.5'],
        ['2012-05-09', '86.64', '87.84', '85.98', '87.27', '3192800', '86.6'],
        ['2012-05-08', '87.12', '87.88', '86.30', '87.56', '2710400', '86.9'],
        ['2012-05-07', '87.70', '88.62', '87.70', '88.01', '2753500', '87.4'],
        ['2012-05-04', '89.17', '89.24', '88.30', '88.67', '2823800', '88.0'],
        ['2012-05-03', '89.50', '89.85', '89.11', '89.39', '2343600', '88.7'],
        ['2012-05-02', '89.16', '89.59', '88.89', '89.52', '2100200', '88.9'],
        ['2012-05-01', '89.13', '89.95', '88.74', '89.60', '2294200', '88.9']
      ]
4

4 回答 4

5

代替:

for i in range(1, 31):

和:

for d in Data[1:31]: #since you have range(1,31). Do Data[1:] if you just want to skip the first
    OneDate.append(d[0])
    OneClose.append(d[4])

这通常发生在Data数组的索引少于 31 个时。还要确保d数组至少有 5 个项目,否则d[4]也会抛出类似的错误。采用:

if len(d) >= 5:  #check first.
    OneDate.append(d[0])
    OneClose.append(d[4])
于 2012-07-16T08:27:33.727 回答
0

可能会发生两件事Data[i][4]

  • 该列表Data在 position 没有元素i
  • 该列表Data[i]在 position 没有元素4

Python 会IndexError在这两种情况下引发一个。您是否检查过您的数据是否有效,并且不会发生上述情况?

可能是您的文本文件中缺少数据,ystockquote.get_historical_prices并且无法使用预期格式的数据创建列表。

于 2012-07-16T08:26:55.687 回答
0

的元素之一Data少于 5 项。

你的代码有

OneDate.append(Data[i][0])
OneClose.append(Data[i][4])

并且,第二行是例外。Data因此,您可以确定在, 在 position中肯定有一个元素i。(因为,如果不存在,异常将在第一行本身)。

所以,错误是因为Data[i]没有 5 个元素。(例如,如果 Data[i] 是一个字符串,它的字符数少于 5 个)。

您的解决方案可能是Data[i]在尝试读取它们之前检查它们的长度并确保它有足够的元素。

于 2012-07-16T09:00:36.863 回答
0

当代码触发并同时交换数据更新时会发生此错误。换句话说,当数据在恰好 5、10 或 60 分钟收盘时更新每个报价时,实时更新将存在毫秒差距,因此在该毫秒时间范围内列表将为空。并且因此发生此错误。

于 2020-08-15T11:37:00.403 回答