-1
這是我的代碼。我正在學習python,這是視頻中的練習代碼之一。但我得到的是我無法理解Keyerror(key)from none(python)
def bytespdate2num(fmt, encoding='utf-8'):
strconverter = mdates.strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter
def graph_data(stock):
ax1 = plt.subplot2grid((1,1), (0,0))
stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv'
source_code = urllib.request.urlopen(stock_price_url).read().decode()
stock_data = []
split_source = source_code.split('\n')
for line in split_source:
split_line = line.split(',')
if len(split_line) == 6:
if 'values' not in line and 'labels' not in line:
stock_data.append(line)
date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data,
delimiter=',',
unpack=True,
converters={0: bytespdate2num('%Y%m%d')})
ax1.plot_date(date, closep,'-', label='Price')
for label in ax1.xaxis.get_ticklabels():
label.set_rotation(45)
ax1.grid(True)#, color='g', linestyle='-', linewidth=5)
plt.xlabel('Date')
plt.ylabel('Price')
plt.title('Interesting Graph\nCheck it out')
plt.legend()
plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0)
plt.show()
graph_data('TSLA')
,我得到的錯誤一個錯誤是這樣的:
Traceback (most recent call last):
File "C:\Users\sponnag1\workspace\DataAnalysisWithPython\DataAnalysis.py", line 19, in <module>
from spyder.utils.site.sitecustomize import matplotlib
File "C:\Users\sponnag1\AppData\Local\Continuum\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 225, in <module>
if os.environ["QT_API"] == 'pyqt':
File "C:\Users\sponnag1\AppData\Local\Continuum\Anaconda3\lib\os.py", line 725, in __getitem__
raise KeyError(key) from None
KeyError: 'QT_API'
如何面對呢?
修復您的格式。您已將代碼作爲文本懸掛在代碼塊的上方和下方,並且還需要格式化異常消息。 –
'QT_API'意味着PyQt,它是一個matplotlib可能試圖繪製的圖形庫... –
'os.environ [「QT_API」]'表示它試圖訪問一個名爲QT_API的環境變量。你所得到的KeyError意味着這個變量沒有被定義。事實上,你是否安裝了Qt?似乎spyder包真的想要它。 –