什么是TypeError: 'NoneType'对象是不可迭代的意思?例子:
for row in data: # Gives TypeError!
print(row)
什么是TypeError: 'NoneType'对象是不可迭代的意思?例子:
for row in data: # Gives TypeError!
print(row)
当前回答
因为使用for循环的结果只是一个值,而不是一组值
pola.py
@app.route("/search")
def search():
title='search'
search_name = request.form.get('search')
search_item = User.query.filter_by(id=search_name).first()
return render_template('search.html', title=title, search_item=search_item )
search.html(错误的)
{% for p in search %}
{{ p }}
search.html(正确)
<td>{{ search_item }}</td>
其他回答
这也取决于你使用的Python版本。看到python 3.6和python 3.8中抛出的不同错误消息,如下所示,这是我的情况下的问题
Python 3.6
(a,b) =无 回溯(最近一次调用): 文件“<stdin>”,第1行,在<模块> 'NoneType'对象不可迭代
Python 3.8
(a,b) =无 回溯(最近一次调用): 文件“<stdin>”,第1行,在<模块> TypeError:不能解包不可迭代的NoneType对象
这意味着data的值为None。
因为使用for循环的结果只是一个值,而不是一组值
pola.py
@app.route("/search")
def search():
title='search'
search_name = request.form.get('search')
search_item = User.query.filter_by(id=search_name).first()
return render_template('search.html', title=title, search_item=search_item )
search.html(错误的)
{% for p in search %}
{{ p }}
search.html(正确)
<td>{{ search_item }}</td>
当你得到None Exception时,继续循环,
例子:
a = None
if a is None:
continue
else:
print("do something")
这可以是来自DB或excel文件的任何可迭代对象。
表示data的值为None。