Skip to main content
Question

Set a list attribute in Python

  • August 28, 2013
  • 4 replies
  • 203 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

4 replies

takashi
Influencer
  • 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
Influencer
  • 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.

Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings