Skip to main content
Solved

How do I loop over list elements for in python caller?

  • December 19, 2017
  • 4 replies
  • 125 views

Forum|alt.badge.img

I have an aggregator transformer (to aggregate incoming features because I want a single feature to enter the subsequent Emailer transformer). I'm trying to use the python code (in PythonCaller transformer) to extract info about each of the aggregated features for output to the email. I'm using "for x in range (0, feature.getAttribute('Count')-1)" to try to loop over the lists but the workspace isn't accepting x.

Best answer by jdh

for x in range (0, feature.getAttribute('Count')-1) will not include the last item in the list, I don't know if this is intentional or not.

the @Value(list{x}.Value) syntax you get when double clicking on a list attribute in the FME Feature Attributes panel is garbage. If you select the attribute and drag and drop in the code pane you'll end up with the correct syntax of feature.getAttribute('_list{#}.Value').

In your particular case you'll probably want something like feature.getAttribute('Details{%d}.ACCOUNT_NO' % x)

 

 

Alternatively you could have a loop like

acntList = feature.getAttribute('Details{}.ACCOUNT_NO')

 

for i, x in enumerate(acntList):

[...]

feature.getAttribute('Details{%d}.LOCATION_NO' % i)

[...]

View original
Did this help you find an answer to your question?

4 replies

Forum|alt.badge.img
  • Author
  • December 19, 2017
I've modified the close method in my python (as I had an error in there) but that doesn't solve my looping issue of course.

 

pythoncode.png

 

 


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • Best Answer
  • December 19, 2017

for x in range (0, feature.getAttribute('Count')-1) will not include the last item in the list, I don't know if this is intentional or not.

the @Value(list{x}.Value) syntax you get when double clicking on a list attribute in the FME Feature Attributes panel is garbage. If you select the attribute and drag and drop in the code pane you'll end up with the correct syntax of feature.getAttribute('_list{#}.Value').

In your particular case you'll probably want something like feature.getAttribute('Details{%d}.ACCOUNT_NO' % x)

 

 

Alternatively you could have a loop like

acntList = feature.getAttribute('Details{}.ACCOUNT_NO')

 

for i, x in enumerate(acntList):

[...]

feature.getAttribute('Details{%d}.LOCATION_NO' % i)

[...]


Forum|alt.badge.img
  • Author
  • December 20, 2017
jdh wrote:

for x in range (0, feature.getAttribute('Count')-1) will not include the last item in the list, I don't know if this is intentional or not.

the @Value(list{x}.Value) syntax you get when double clicking on a list attribute in the FME Feature Attributes panel is garbage. If you select the attribute and drag and drop in the code pane you'll end up with the correct syntax of feature.getAttribute('_list{#}.Value').

In your particular case you'll probably want something like feature.getAttribute('Details{%d}.ACCOUNT_NO' % x)

 

 

Alternatively you could have a loop like

acntList = feature.getAttribute('Details{}.ACCOUNT_NO')

 

for i, x in enumerate(acntList):

[...]

feature.getAttribute('Details{%d}.LOCATION_NO' % i)

[...]

Great, thanks!

 

 


takashi
Influencer
  • December 20, 2017

Hi @ieukcoca, there should be several variations on scripting, but I don't think it's essential to write a Python script in this case. I think this workflow works for you as well.


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