TypeError: 'NoneType' object is not iterable in Python

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

What does error TypeError: 'NoneType' object is not iterable mean?

I am getting it on this Python code:

def write_file(data,filename):   #creates file and writes list to it   with open(filename,'wb') as outfile:     writer=csv.writer(outfile)     for row in data:             ##ABOVE ERROR IS THROWN HERE       writer.writerow(row)

回答1:

It means "data" is None.



回答2:

Code: for row in data:
Error message: TypeError: 'NoneType' object is not iterable

Which object is it complaining about? Choice of two, row and data. In for row in data, which needs to be iterable? Only data.

What's the problem with data? Its type is NoneType. Only None has type NoneType. So data is None.

You can verify this in an IDE, or by inserting e.g. print "data is", repr(data) before the for statement, and re-running.

Think about what you need to do next: How should "no data" be represented? Do we write an empty file? Do we raise an exception or log a warning or keep silent?



回答3:

How to reproduce this error in python:

Python methods will return NoneType if you expect a tuple from them and fail to return anything to fill them up:

>>> def baz(): ...   print("k") ...  >>> a, b = baz() k Traceback (most recent call last):   File "", line 1, in  TypeError: 'NoneType' object is not iterable

You can also get that error if you assign NoneType to a variable:

>>> a = NoneType Traceback (most recent call last):   File "", line 1, in  NameError: name 'NoneType' is not defined

If you try to iterate NoneType in a for loop, you get that error:

>>> for i in NoneType: ...   print("Yeah") ...  Traceback (most recent call last):   File "", line 1, in  NameError: name 'NoneType' is not defined

Try to concatenate None and a string, you get that error:

>>> bar = "something" >>> foo = None >>> print foo + bar TypeError: cannot concatenate 'str' and 'NoneType' objects

If you use a variable passed in a method that contains NoneType, you get that error:

>>> def foo(data): ...   print(data) ...  >>> foo(NoneType) Traceback (most recent call last):   File "", line 1, in  NameError: name 'NoneType' is not defined

What's going on under the hood here? Who what when where why how?

The python interpreter converted your above code to pyc bytecode and then the Python virtual machine's line of execution encountered your for loop which invoked the __iter__ method on the variable called data.

data has the value None, which clearly has no __iter__ method, so the Python virtual machine is telling you what it sees: that NoneType object you fed it does not have a __iter__ method which hands me back an iteratable.

This is why Python's duck-typing is considered bad. You do something completely reasonable, and a completely reasonable exception comes along and the python virtual machine pukes up a bunch of unrelated nonsense all over the carpet.

Java doesn't have these problems because such a program wouldn't even compile since you haven't defined your return types and haven't specified what to do during an exception.



回答4:

It means that the data variable is passing None (which is type NoneType), its equivalent for nothing. So it can't be iterable as a list, as you are trying to do.



回答5:

You're calling write_file with arguments like this:

write_file(foo, bar)

But you haven't defined 'foo' correctly, or you have a typo in your code so that it's creating a new empty variable and passing it in.



回答6:

Another thing that can produce this error is when you are setting something equal to the return from a function, but forgot to actually return anything.

Example:

def foo(dict_of_dicts):     for key, row in dict_of_dicts.items():         for key, inner_row in row.items():             Do SomeThing     #Whoops, forgot to return all my stuff  return1, return2, return3 = foo(dict_of_dicts)

This is a little bit of a tough error to spot because the error can also be produced if the row variable happens to be None on one of the iterations. The way to spot it is that the trace fails on the last line and not inside the function.

If your only returning one variable from a function, I am not sure if the error would be produced... I suspect error "'NoneType' object is not iterable in Python" in this case is actually implying "Hey, I'm trying to iterate over the return values to assign them to these three variables in order but I'm only getting None to iterate over"



回答7:

It can happen if the fieldlist for the DictWriter is None!



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