Solved

'NoneType' object is not iterable


i have two input tables and i am trying to find all matches that start with attribute 'partial' from the first input

in the attribute 'TAG_ID' from the second input

so i converted all 'TAG_ID' to a list 'tag_list{}.TAG_ID' but when i tried to iterate to do so the error

'NoneType' object is not iterable

appear to me

here's my code

        s = str(feature.getAttribute('partial'))
        d =feature.getAttribute('tag_list{}.TAG_ID')
        matches = [i for i in d if s in i] 

 

icon

Best answer by mohammedkh 29 April 2020, 14:10

View original

22 replies

Userlevel 4

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

@david_r 

will its a list of strings 

The value of "d" is: ['40QFM1818918004', '40QFM2615910278', '40QFM1322416717', '40QFM2059919336', '40QFM1539918653']

its very huge there a sample

Userlevel 1
Badge +10

Should all features have the tag_list list? If you're happy to ignore cases where the list doesn't exist you could use. Or you could just test to exclude prior to the pythoncaller

    s = str(feature.getAttribute('partial'))
    d = feature.getAttribute('tag_list{}.TAG_ID')
    if d:
        matches = [i for i in d if s in i]
Userlevel 4

@david_r 

will its a list of strings 

The value of "d" is: ['40QFM1818918004', '40QFM2615910278', '40QFM1322416717', '40QFM2059919336', '40QFM1539918653']

its very huge there a sample

I'm guessing you have more than one feature? Do they all have a list of values, or does one or several of them not have a list value? 

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

no this list is made with listbuilder because i didn't how to do it any other way 

Userlevel 4

no this list is made with listbuilder because i didn't how to do it any other way

Before your Python code, try inserting a ListElementCounter on tag_list{}.TAG_ID, then a Tester to check that the count of elements > 0. If you have any elements where the count is 0, then there's the problem.

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

its > 0 it passed the tester

and i tried to use ListElementCounter too loop with while but it also gave me some error about 'NoneType'  

Userlevel 1
Badge +10

its > 0 it passed the tester

and i tried to use ListElementCounter too loop with while but it also gave me some error about 'NoneType'

If your list has sub_elements it's possible for it to pass the ListElementCounter test whilst still not having a value for tag_list{}.TAG_ID depending on how the list has been built

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

only one attribute is add in the list on ListBuilder parameters if that's what you mean 

Userlevel 1
Badge +10

If you want to find which features do not have any tag_list{}.TAG_ID values you could do the following in a python caller

0684Q00000ArKuQQAV.png

 

import fme
import fmeobjects

class FeatureProcessor(object):
    def __init__(self):
        pass
    def input(self,feature):
        s = str(feature.getAttribute('partial'))
        d = feature.getAttribute('tag_list{}.TAG_ID')
        if d:
            pass
        else:
            self.pyoutput(feature)
    def close(self):
        pass
Userlevel 1
Badge +10

only one attribute is add in the list on ListBuilder parameters if that's what you mean

Can you share a screenshot of your listbuilder?

Seems like the value of 'd' is None at some point, for some reason.

Try to comment out the third line and add the following to see what the value actually is while the workspace is running:

print('The value of "d" is: ' + str(d))

yeah sure here

0684Q00000ArMcFQAV.png

Should all features have the tag_list list? If you're happy to ignore cases where the list doesn't exist you could use. Or you could just test to exclude prior to the pythoncaller

    s = str(feature.getAttribute('partial'))
    d = feature.getAttribute('tag_list{}.TAG_ID')
    if d:
        matches = [i for i in d if s in i]

i tried this but it skips everything like the list is with empty elements @ebygomm 

i think the problem is that i put the list(coming from listbuilder) as input and the pythoncaller treating it as the firs input not something i need to use with each record

 

@ebygomm @david_r what should i do to make the list as a variable usable with every record in paythoncaller

Userlevel 1
Badge +10

i think the problem is that i put the list(coming from listbuilder) as input and the pythoncaller treating it as the firs input not something i need to use with each record

 

@ebygomm @david_r what should i do to make the list as a variable usable with every record in paythoncaller

Can you share a screenshot of the pythoncaller?

Userlevel 4

Is it possible that your code is located under the "__init__" method (executed once at startup) rather than under the "input" method (executed once for every input feature)?

See the screenshot posted by @ebygomm below.

 

 

Is it possible that your code is located under the "__init__" method (executed once at startup) rather than under the "input" method (executed once for every input feature)?

See the screenshot posted by @ebygomm below.

 

 

no it is under input

Userlevel 4

no it is under input

Can you please post a screenshot of the entire PythonCaller transformer configuration? Or even better, post a minimal sample workspace that demonstrates the problem.

I couldn't solve the problem so i made a pythoncaller after the listbuilder and in it i store the list as a global variable and it worked

This error means that python is trying to iterate over a None object. With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: ‘NoneType’ object is not iterable error.

 

For example, list.sort() only change the list but return None. 

 

Python's interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the `__iter__` method on the None. None has no` __iter__` method defined, so Python's virtual machine tells you what it sees: that NoneType has no` __iter__` method.

 

Technically, you can avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.

 

 

 

Userlevel 4

This error means that python is trying to iterate over a None object. With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: ‘NoneType’ object is not iterable error.

 

For example, list.sort() only change the list but return None. 

 

Python's interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the `__iter__` method on the None. None has no` __iter__` method defined, so Python's virtual machine tells you what it sees: that NoneType has no` __iter__` method.

 

Technically, you can avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.

 

 

 

I'll just add that not all objects are iterable, e.g. int and float types aren't iterable, meaning that simply checking for "not None" isn't quite enough. See also:

https://www.geeksforgeeks.org/how-to-check-if-an-object-is-iterable-in-python/

This error indicates an attempt to access an indexing functionality on an object that does not support it. Specifically, you are trying to subscript an object that you assume to be a list or dictionary, but it is actually None. NoneType is the type assigned to the None object, which signifies the absence of a value. For instance, a function that does not explicitly return a value will default to returning None. When you encounter the error message "'NoneType' object is not subscriptable," it means that you are using square bracket notation (object[key]) on an object that does not define the getitem method required for indexing.

 

A subscriptable object refers to any object that supports the getitem special method, such as lists or dictionaries. It is an object that can record and store operations performed on it as a "script" that can be replayed. However, in your case, you are attempting to subscript an object that you believe to be a list or dictionary, but it is actually None. NoneType is the type assigned to the None object, which signifies the absence of a value. For instance, when a function does not explicitly return a value, it defaults to returning None. When you encounter the error "'NoneType' object is not subscriptable," it means that you are using square bracket notation (object[key]) on an object that does not define the getitem method required for indexing. It is worth noting that certain methods, like sort(), which only modify a list, do not have a printed return value and instead return the default None. This is a design principle applied to all mutable data structures in Python.

 

 

Reply