Error when configuring tkinter widget: ‘NoneType’ object has no attribute

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x