Python 配置tkinter小部件时出错:';非类型';对象没有属性
我正在运行以下代码,当我硬编码值时,这些代码运行良好Python 配置tkinter小部件时出错:';非类型';对象没有属性,python,nonetype,Python,Nonetype,我正在运行以下代码,当我硬编码值时,这些代码运行良好 from nsetools import Nse nse = Nse() with open('all_nse_stocks') as nse_stocks: for stock in nse_stocks: q = nse.get_quote('INFY') print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '
from nsetools import Nse
nse = Nse()
with open('all_nse_stocks') as nse_stocks:
for stock in nse_stocks:
q = nse.get_quote('INFY')
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
请注意,我已经硬编码了nse.get_quote('INFY')的值
但是当我运行以下代码时,我得到以下错误:
from nsetools import Nse
nse = Nse()
with open('all_nse_stocks') as nse_stocks:
for stock in nse_stocks:
q = nse.get_quote(stock)
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
错误:
Traceback (most recent call last):
File "test.py", line 6, in <module>
print q.get('open'), '\t', q.get('lastPrice'), '\t', q.get('dayHigh'), '\t', q.get('dayLow')
AttributeError: 'NoneType' object has no attribute 'get'
回溯(最近一次呼叫最后一次):
文件“test.py”,第6行,在
打印q.get('open')、'\t',q.get('lastPrice')、'\t',q.get('dayHigh')、'\t',q.get('dayLow'))
AttributeError:“非类型”对象没有属性“get”
请帮助
非类型对象没有属性…
表示您有一个对象是None
,并且您正在尝试使用该对象的属性
在您的例子中,您正在执行q.get(…)
,因此q
必须是None
。由于q
是调用nse.get_quote(…)
的结果,因此该函数必须能够返回None
。您需要调整代码以考虑这种可能性,例如在尝试使用之前检查结果:
q = nse.get_quote(stock)
if q is not None:
print ...
问题的根源可能在于如何读取文件<代码>股票将包含换行符,因此您应该在调用nse之前去掉换行符。获取报价:
q = nse.get_quote(stock.strip())
请检查“库存”的类型 q=nse.获取报价(股票) 它一定是一根绳子。另外,nestools仅在Python2上受支持,您尚未澄清您的python版本
如果您在阅读时仍面临此问题,请告诉我。q=nse.get_quote(stock)返回无。检查哪些nse股票可用。所有nse股票是什么样子的?所有nse股票是一个包含INFYHello Bryan的文件,我在所有nse股票中只有一个值,即INFY。如果我硬编码,那么我得到的是正确的输出。问题只是当我从中读取值时file@user3198755:你试过调试吗?例如,在获取报价之前打印出
stock
的值。是的,在For循环之后,我尝试打印stock值,它给出了正确的值,即。INFY@user3198755:您确定它确实是INFY
而不是INFY
?它可能有一条新线,你需要去掉它。我已经更新了我的答案,以说明如何更新。我已经仔细检查了。它的结尾没有任何新行字符。nse.get_quote('INFY')起作用,但nse.get_quote(stock)不起作用。这和单引号有关吗?INFY和“INFY”?