python中的错误-“NoneType”类型的对象没有len()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44187755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:48:29  来源:igfitidea点击:

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

pythonsyntax-error

提问by Alex

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

我不确定我的 python 代码有什么问题:

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

错误信息是: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):

有什么建议吗?当我的查询没有结果时,我想显示一条 Flash 消息。我也试过(但没有用):

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)

回答by David Cullen

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

您正在尝试获取len(None). 你想要的是

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

Note:Python does not have a nokeyword. The closest thing is the notoperator.

注意:Python 没有no关键字。最接近的是not运营商。