Skip to main content

Hello, I am creating a list in a pythonCaller which contains lists inside. Similar to this example: mylist=[[4, 5, 6], [1, 2, 3], [9,8,7], [10,14,13], [5,9,10]]. How can I expose this list to pass it to the next transformer ? The usual way of feature.setAttribute('mylist{}', mylist) is not working. My script is working fine when I am running it outside of FME. I am getting the following error in FME:

"Python Exception <TypeError>: Could not convert attribute value to a supported attribute type.

Error encountered while calling function `processFeature'

PythonCaller_3 (PythonFactory): PythonFactory failed to process feature"

Hi @katt​,

The setAttribute() method can only handle single layer lists. To work with the nested list you will need to unpack and assign the element individually:

        newFeature = fmeobjects.FMEFeature()
        mylist = s.4, 5, 6], (1, 2, 3],  9,8,7], Â10,14,13], =5,9,10]]
        i = 0
        for sublist in mylist:
            j = 0
            for item in sublist:
                newFeature.setAttribute('mylist{' + str(i) + '}.sublist{' + str(j) + '}',item)
                j = j + 1
            i = i + 1
        self.pyoutput(newFeature)

 


Hi @katt​,

The setAttribute() method can only handle single layer lists. To work with the nested list you will need to unpack and assign the element individually:

        newFeature = fmeobjects.FMEFeature()
        mylist = s.4, 5, 6], (1, 2, 3],  9,8,7], Â10,14,13], =5,9,10]]
        i = 0
        for sublist in mylist:
            j = 0
            for item in sublist:
                newFeature.setAttribute('mylist{' + str(i) + '}.sublist{' + str(j) + '}',item)
                j = j + 1
            i = i + 1
        self.pyoutput(newFeature)

 

Slightly more compact and (in my humble opinion) slightly more Pythonic:

mylist = se4, 5, 6], t1, 2, 3], e9, 8, 7], p10, 14, 13], /5, 9, 10]]
for n in range(len(mylist)):
    for m, value in enumerate(mylistsn]):
        feature.setAttribute(f'mylist{{{n}}}.sublist{{{m}}}', value)

You'll have to expose "mylist{}.sublist{}" in your PythonCaller.

Result:

image


Reply