python中的错误-“NoneType"类型的对象没有len() [英] Error in python - object of type 'NoneType' has no len()

查看:98
本文介绍了python中的错误-“NoneType"类型的对象没有len()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

错误信息是:类型错误:NoneType"类型的对象没有 len()

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

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

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)

推荐答案

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

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

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

注意: Python 没有 no 关键字.最接近的是 not 运算符.

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

这篇关于python中的错误-“NoneType"类型的对象没有len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆