我正在试着写一个简单的程序,它使用csv中的股票列表。我使用的是名为nsetools的库,它为印度股市提供股票信息。Loop应从csv中挑选股票代码,并打印股票的当前市场价格。但是,我得到了以下错误:
Traceback (most recent call last):
File "C:/Users/anoopmah/AppData/Roaming/JetBrains/PyCharmCE2020.3/scratches/scratch_7.py", line 15, in <module>
p = nse.get_quote('temp')['lastPrice']
TypeError: 'NoneType' object is not subscriptable代码:
import nsetools
import pandas as pd
from nsetools import Nse
nse = Nse()
#Import CSV into dataframe
filepath = "C:/temp/Stock/stocklist.csv"
df = pd.read_csv(filepath,encoding='windows-1252')
for i in range(0,len(df)):
temp = df["Name"][i]
print(temp)
p = nse.get_quote('temp')['lastPrice']
print(p)发布于 2021-03-14 03:50:53
TypeError: 'NoneType' object is not subscriptable错误指出您试图下标(使用[])一个没有下标功能的对象。在本例中,它表示试图下标一个值为None (因此为... 'NoneType' object ...)的对象。
如果我们检查发生错误的行,您会看到下面这行代码:
p = nse.get_quote('temp')['lastPrice']这里发生的事情是,您试图从报价'temp‘中检索数据,然后获取与关键字(在数据字典中) 'lastPrice’匹配的值。这样做的问题在于,函数get_quote可能会返回None。正如您所看到的,在documentation中指定“如果您在无效的代码上执行get_quote或get_index_quote,那么API将返回None。它不会像预期的那样引发异常。”
因此,您可以得出结论,由于'temp‘是一个无效的引号而导致了bug的发生。
https://stackoverflow.com/questions/66617676
复制相似问题