什么是TypeError: 'NoneType'对象是不可迭代的意思?例子:
for row in data: # Gives TypeError!
print(row)
什么是TypeError: 'NoneType'对象是不可迭代的意思?例子:
for row in data: # Gives TypeError!
print(row)
当前回答
代码:用于数据中的行: TypeError: 'NoneType'对象不可迭代
它在抱怨哪个对象?二选一,行和数据。 在数据中的for行,需要是可迭代的?只有数据。
数据有什么问题?它的类型是NoneType。只有None类型为NoneType。所以data是None。
你可以在IDE中验证这一点,或者在for语句之前插入print "data is", repr(data),然后重新运行。
想想你接下来需要做什么: “无数据”应该如何表示?我们写一个空文件吗?我们是引发异常,还是记录警告,还是保持沉默?
其他回答
因为使用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。
你用这样的参数调用write_file:
write_file(foo, bar)
但是你没有正确地定义'foo',或者你在你的代码中有一个拼写错误,所以它创建了一个新的空变量并传入它。
这意味着data的值为None。