Skip to main content
Solved

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

  • December 19, 2017
  • 4 replies
  • 208 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)

[...]

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

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+40
  • 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

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
Celebrity
  • 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.