Python IndexError: list index out of range
문제
I'm attempting to get Python to populate the lists with the closing prices of 500 stocks. While the code seems to work fine for just a few stocks, the large amount poses a problem. Python keeps giving me the following error: "OneClose.append(Data[i][4]) IndexError: list index out of range.
" I'm not sure how to remedy this problem. No matter what I seem to enter in the range field (31 in this instance), the error persists.
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
contains:
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']
]
해결책
Replace:
for i in range(1, 31):
with:
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])
This usually happends when Data
array has less than 31 indices. Also ensure d
array has atleast 5 items, else d[4]
will also throw a similar error. Use:
if len(d) >= 5: #check first.
OneDate.append(d[0])
OneClose.append(d[4])
다른 팁
Two things can occur with Data[i][4]
:
- The list
Data
does not have an element at positioni
. - The list
Data[i]
does not have an element at position4
.
Python will raise an IndexError
in these two situations. Have you checked that your data is valid and that the above situations won't occur?
It could be that data is missing in your text file and that ystockquote.get_historical_prices
is unable to create a list with the data in the expected format.
One of the elements of Data
has less than 5 items.
Your code has
OneDate.append(Data[i][0])
OneClose.append(Data[i][4])
and, the exception is in the second line. So, you can be sure that there definitely is an element in Data
, at position i
. (because, if not present, the exception would have been at the first line itself).
So, the error is because Data[i]
does not have 5 elements.
(eg. if Data[i] is a string, it has less than 5 characters in it).
You solution could be to check the length of Data[i]
and making sure that it has enough elements, before trying to read them.
this error happens when code trigger and exchange data updation at same time. in other terms when data updates fresh every quotes at exactly 5, 10 or 60 mins close bar, there will be milli seconds gap in real time update, so list will be empty in that milli seconds time frame. and this error happens due to that.