Question

Output dictionary key and value pair from PythonCaller


Badge +1

Hi,

I have created a series of Dictionaries in the PythonCaller and I want to output the Key and Value pairs as new attributes with values for each feature.

Is there syntax using feature.setAttribute to do this?


14 replies

Userlevel 4

Try something like this

for key, value in mydict.items():
    feature.setAttribute(key, value)
Badge +22

for k,v in d.items():
feature.setAttribute(k,v)
Note that you will have to explicitly expose the attributes to use them in the workspace

Badge +22

Try something like this

for key, value in mydict.items():
    feature.setAttribute(key, value)
great minds and all that...

 

 

Badge +1

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

Userlevel 4

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

Even if you could expose them, what would you like do with the attribute names since they're not known when you're designing the workspace?
Badge +1

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

I would like to populate them with values found in a list.

 

 

My overall issue is that I have a number of lists. Inside one of the lists is the values that I would like to use as column headers in an excel spreadsheet and the values that will be under the column headers can be found in another one of the lists. I can't work out how to make the values in the list into column headers.

 

 

Perhaps there is another way to do this??

 

Userlevel 4
I would like to populate them with values found in a list.

 

 

My overall issue is that I have a number of lists. Inside one of the lists is the values that I would like to use as column headers in an excel spreadsheet and the values that will be under the column headers can be found in another one of the lists. I can't work out how to make the values in the list into column headers.

 

 

Perhaps there is another way to do this??

 

You'll have to use a writer configured for dynamic schema based on a schema feature (that you'll have to generate yourself). Look into the SchemaSetter on the FME Hub for a start.
Userlevel 4
I would like to populate them with values found in a list.

 

 

My overall issue is that I have a number of lists. Inside one of the lists is the values that I would like to use as column headers in an excel spreadsheet and the values that will be under the column headers can be found in another one of the lists. I can't work out how to make the values in the list into column headers.

 

 

Perhaps there is another way to do this??

 

This is a great starting point: https://knowledge.safe.com/articles/1051/index.html
Badge +1

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

Thanks David, I have managed to create the new attributes using 'AttributeCreator'

 

 

 

And when I set the Excel Writer Schema Sources to "Schema From Schema Feature" I am outputting an excel spreadsheet with the new column headers as I required.

 

 

However, I can't work out how to populate these column headers! I tried to populate them in AttributeManager like so:

 

 

 

 

But then in the excel spreadsheet the values of the column headers replaced the column headers themselves.

 

 

I seem to be missing one vital step here but I can't work out what it is.

 

Thanks,

 

 

 

Userlevel 4
Thanks David, I have managed to create the new attributes using 'AttributeCreator'

 

 

 

And when I set the Excel Writer Schema Sources to "Schema From Schema Feature" I am outputting an excel spreadsheet with the new column headers as I required.

 

 

However, I can't work out how to populate these column headers! I tried to populate them in AttributeManager like so:

 

 

 

 

But then in the excel spreadsheet the values of the column headers replaced the column headers themselves.

 

 

I seem to be missing one vital step here but I can't work out what it is.

 

Thanks,

 

 

 

I can't see your screenshot... Maybe it's easier if you post it as a separate question.
Badge +1

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

Hi David

 

I tried posting as a new question but there is an issue with my computer and screen shots are not working for some reason.

 

 

https://knowledge.safe.com/questions/56136/creating-a-schemafeature-and-mapping-values-to-it.html?

 

 

In essence my problem is now that I can create the new column headers using the Attribute Creator with the parameters as such:

 

 

New AttributeAttribute Valueattribute{0}.name@Value(_candidates{0}.Node_Label)_Distanceattribute{0}.fme_data_typefme_decimal(10,2)attribute{1}.name@Value(_candidates{1}.Node_Label)_Distanceattribute{1}.fme_data_typefme_decimal(10,2)attribute{2}.name@Value(_candidates{2}.Node_Label)_Distanceattribute{2}.fme_data_typefme_decimal(10,2)But I can't work out how to populate the new attributes names with the actual values (which are coming from another list).

 

Thanks,

 

 

Userlevel 4
Hi David

 

I tried posting as a new question but there is an issue with my computer and screen shots are not working for some reason.

 

 

https://knowledge.safe.com/questions/56136/creating-a-schemafeature-and-mapping-values-to-it.html?

 

 

In essence my problem is now that I can create the new column headers using the Attribute Creator with the parameters as such:

 

 

New AttributeAttribute Valueattribute{0}.name@Value(_candidates{0}.Node_Label)_Distanceattribute{0}.fme_data_typefme_decimal(10,2)attribute{1}.name@Value(_candidates{1}.Node_Label)_Distanceattribute{1}.fme_data_typefme_decimal(10,2)attribute{2}.name@Value(_candidates{2}.Node_Label)_Distanceattribute{2}.fme_data_typefme_decimal(10,2)But I can't work out how to populate the new attributes names with the actual values (which are coming from another list).

 

Thanks,

 

 

The problem is that you can't have functions inside your attribute values here, they aren't automatically evaluated by the writer. This means that you'll have to use the value of "@Value(_candidates{0}.Node_Label)_Distance", not the expression as a string.
Badge +1

Thanks! The only issue is I won't know the name of the keys since they are created as part of the code. Is there anyway to expose them without knowing what they are called?

Sorry I should have made myself clearer.

 

 

The functions inside the Attribute Value field in the AttributeCreator are being calculated correctly. I was able to create new attributes based on those functions. But my problem is that I can't populate the new attributes that were created with any values.

 

 

Thanks

 

 

Userlevel 4
Sorry I should have made myself clearer.

 

 

The functions inside the Attribute Value field in the AttributeCreator are being calculated correctly. I was able to create new attributes based on those functions. But my problem is that I can't populate the new attributes that were created with any values.

 

 

Thanks

 

 

In that case I don't have enough information to help you out, sorry. Maybe consider posting more information (a minimal workspace to reproduce the issue would be best) on the separate question you created.

 

Reply