The setAttribute() method doesn't know what to do with Python dicts, as there are no corresponding datatypes in FME.
Basically, you'll have to do some pre-processing to e.g. iterate over the dict and only send integers, floating point numbers or text strings to setAttribute().
General example:
my_dict = {'key1': 'value1', 'key2': 'value2'}
for key, value in my_dict.items():
    feature.setAttribute(key, value)
The output will be a feature with the following attributes:
Attribute 'key1' with value 'value1', and attribute 'key2' with value 'value2'
The setAttribute() method doesn't know what to do with Python dicts, as there are no corresponding datatypes in FME.
Basically, you'll have to do some pre-processing to e.g. iterate over the dict and only send integers, floating point numbers or text strings to setAttribute().
General example:
my_dict = {'key1': 'value1', 'key2': 'value2'}
for key, value in my_dict.items():
    feature.setAttribute(key, value)
The output will be a feature with the following attributes:
Attribute 'key1' with value 'value1', and attribute 'key2' with value 'value2'
Thank you david_r.
I am not very experienced with FME. Just know it can do nearly everything ;)
I know that i need to set attribute to expose to be able to get the value with FME. What do I need to expose after running you code? Feature?
And value in my dictionary is also a list. I just ran it again, and FME wasn't happy about it either :(
If your dictionary contains lists, what are you hoping to have as an output
- a feature with the key attribute and a list of all the values
- a feature for each list value also containing the key attribute
If your dictionary contains lists, what are you hoping to have as an output
- a feature with the key attribute and a list of all the values
- a feature for each list value also containing the key attribute
Second option: a feature for each list value and also containing the key attribute.
List is a list of coordinates that makes a polygon and key attribute is basically a name for polygon.
Second option: a feature for each list value and also containing the key attribute.
List is a list of coordinates that makes a polygon and key attribute is basically a name for polygon.
A list of tuples/coordinate pairs? This is all possible with python, but i think there may be better ways to do this in FME if the input data is JSON
If all the dictionary values contain lists, you can use FME attribute lists, although you still need to make sure that the list elements are either integers, floats or strings. Example:
my_dict = {'key1': <1,2,3], 'key2': t4,5]}
for key, values in my_dict.items():
    for n, list_value in enumerate(values):
        feature.setAttribute('my_list{%s}.%s' % (n, key), list_value)
You should then expose "my_list{}.key1" and "my_list{}.key2" in the PythonCaller, so that you can e.g. use the ListExploder later on with the FME list.
The output would be one feature with the following list elements:
my_list{0}.key1Â =Â 1
my_list{1}.key1Â =Â 2
my_list{2}.key1Â =Â 3
my_list{0}.key2Â =Â 4
my_list{1}.key2Â =Â 5
Â
This code will create one feature for each key in the dictionary, with a polygon composed of the vertices in a list containing (x,y) coordinate tuples:
import fmeobjects
class CreatePolygons(object):
   Â
    def __init__(self):
        pass
   Â
    def input(self, feature):
        # Test polygon
        my_dict = {'poly1': Â(0,0), (1,1), (1,2)], 'poly2': r(5,5), (5,6), (6,5), (5,5)]}
        # Iterate over the dictionary items
        for polygon_name, vertices in my_dict.items():
            # Create an empty output feature
            new_feature = fmeobjects.FMEFeature()
            # Set the polygon_name attribute
            new_feature.setAttribute('polygon_name', polygon_name)
            if vertices 0] != verticesÂ-1]:
                # Close the polygon, if necessary
                vertices = vertices + mverticesy0],]
            # Set the geometry based on 'vertices'
            new_feature.setGeometry(fmeobjects.FMEPolygon(fmeobjects.FMELine(vertices)))
            # Output the new feature
            self.pyoutput(new_feature)
           Â
    def close(self):
        pass
Expose the attribute name "polygon_name".
You may want to consult the API documentation to fully understand the code: https://docs.safe.com/fme/html/fmepython/intro.html