2
votes

I am running the below code which run fine when I hard code the value

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')

see that I have hard-coded the value nse.get_quote('INFY') But when I run the following code, I get the following error:

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')

ERROR:

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'

Please help

2
q = nse.get_quote(stock) returns a None. Check which nse_stocks are available. - RvdK
What does all_nse_stocks look like? - MattDMo
all_nse_stocks is a file which contains INFY - user3198755

2 Answers

6
votes

NoneType object has no attribute ... means that you have an object that is None, and you're trying to use an attribute of that object.

In your case you're doing q.get(...), so q must be None. Since q is the result of calling nse.get_quote(...), that function must have the possibility of returning None. You'll need to adjust your code to account for that possibility, such as checking the result before trying to use it:

q = nse.get_quote(stock)
if q is not None:
    print ...

The root of the problem is likely in how you're reading the file. stock will include the newline, so you should strip that off before calling nse.get_quote:

q = nse.get_quote(stock.strip())
0
votes

Please check the type of 'stock' in q = nse.get_quote(stock)

it must be a string. Also nestools is only supported on Python2, you have not clarified about your python version.

If you are still facing the issue by the time of reading it, please let me know.