Error in python - object of type 'NoneType' has no len()

末鹿安然 提交于 2020-02-27 08:21:08

问题


I am not sure what is wrong with my python code:

geneid=request.args.get('geneid')
sql=text('select * from INFO where name=:ident')
genes=engine.execute(sql,ident=geneid).fetchone()
params['objs']=genes
if len(genes)==0:
    flash('NO RESULTS')
return render_template('info.html', **params)

The error message is: TypeError: object of type 'NoneType' has no len()

Any suggestion? I would like to show a flash message when there is no result in my query. I tried also (but did not work):

geneid=request.args.get('geneid')
sql=text('select * from INFO where name=:ident')
genes=engine.execute(sql,ident=geneid).fetchone()
params['objs']=genes
if no genes:
    flash('NO RESULTS')
return render_template('info.html', **params)

回答1:


You are trying to get len(None). What you want is

if genes is None:
    flash('NO RESULTS')

Note: Python does not have a no keyword. The closest thing is the not operator.



来源:https://stackoverflow.com/questions/44187755/error-in-python-object-of-type-nonetype-has-no-len

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!