Skip to main content
Question

Output dictionary key and value pair from PythonCaller

  • October 18, 2017
  • 14 replies
  • 137 views

Forum|alt.badge.img+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?

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

14 replies

david_r
Celebrity
  • 8394 replies
  • October 18, 2017

Try something like this

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

jdh
Contributor
Forum|alt.badge.img+37
  • Contributor
  • 2002 replies
  • October 18, 2017

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


jdh
Contributor
Forum|alt.badge.img+37
  • Contributor
  • 2002 replies
  • October 18, 2017

Try something like this

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

 

 


Forum|alt.badge.img+1
  • Author
  • 271 replies
  • October 19, 2017

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?


david_r
Celebrity
  • 8394 replies
  • October 19, 2017

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?

Forum|alt.badge.img+1
  • Author
  • 271 replies
  • October 19, 2017

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??

 


david_r
Celebrity
  • 8394 replies
  • October 19, 2017
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.

david_r
Celebrity
  • 8394 replies
  • October 19, 2017
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

Forum|alt.badge.img+1
  • Author
  • 271 replies
  • October 19, 2017

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,

 

 

 


david_r
Celebrity
  • 8394 replies
  • October 19, 2017
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.

Forum|alt.badge.img+1
  • Author
  • 271 replies
  • October 19, 2017

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,

 

 


david_r
Celebrity
  • 8394 replies
  • October 19, 2017
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.

Forum|alt.badge.img+1
  • Author
  • 271 replies
  • October 19, 2017

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

 

 


david_r
Celebrity
  • 8394 replies
  • October 19, 2017
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.