Skip to main content
Question

Set a list attribute in Python

  • August 28, 2013
  • 4 replies
  • 237 views

How can I set a list attribute in Python? I can access the list by something like:

 

for i in range(0, numberFeatures):             subscript = "lstGrouped(%s).STRATA_UNIT_AREA" % i              oneStrataUnitArea = int(feature.getAttribute(subscript))

 

 

...but when I try to set the attribute, FME makes a new list:

 

 

for i in range(0, numberFeatures):             feature.setAttribute("lstGrouped(" + str(i) + ").STRATA_UNIT_AREA",str(oneStrataUnitArea))

 

I can make a new list and expose it on the canvas, but FME does not treat it as a real list. That is, I can't explode a list that I made in Python.

 

 

Thanks
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.

4 replies

takashi
Celebrity
  • August 28, 2013
Hi,   The list index should be enclosed by {}. Try this: ---- for i in range(numberFeatures):     feature.setAttribute("lstGrouped{%d}.STRATA_UNIT_AREA" % i, str(oneStrataUnitArea)) ----

 

Takashi

  • August 28, 2013
You are right, it should be curly braces, not parentheses. But when you use getAttribute, you use parentheses.

 

 

Thanks.

takashi
Celebrity
  • August 28, 2013
You can use {} (curly braces) when using getAttribute. I think it's usual manner in FME Python scripting.

 

Additionally, you can get all the elements of a list attribute as a string list at once.

 

list = feature.getAttribute("lstGrouped{}.STRATA_UNIT_AREA")

 

 

Takashi

  • August 28, 2013
Actually I was wrong about the parentheses in getAttribute(). You can use them and it will not raise an exception, but to get the value of a given subscript, you have to use curly braces.