List attributes, e.g. MyList{}, are great for handling repetative data. But I seem to be stuck in a rather basic problem.
I'm trying to trim my list attribute down to a single entry (the first, index 0). I've used ListIndexer, but I can't seem to get it right.
Is there a simple way to accomplish this ? Or do I need to resolve to TCL or Python (if this is at all possible) ?
Cheers
Lars I Nielsen
Best answer by takashi
Mechanism is the same. ----- # Example 1 # Remove list elements except the first element. def trimList(feature): subNames = ['FIRST', 'SECOND'] for sub in subNames: subList = feature.getAttribute('LIST{}.%s' % sub) if subList: for i in range(1, len(subList)): feature.removeAttribute('LIST{%d}.%s' % (i, sub)) ----- Another approach. This works like the ListIndexer, AttributeRemover and AttributeRenamer. If there are many sub names, this might be more efficient. ----- # Example 2 # Save the first element, remove list and recover the first element. def trimList(feature): subNames = ['FIRST', 'SECOND'] for sub in subNames: value = feature.getAttribute('LIST{0}.%s' % sub) feature.removeAttribute('LIST{}.%s' % sub) feature.setAttribute('LIST{0}.%s' % sub, value) ----- Not tested. If there were typos, please correct.
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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.
You will want to use the ListExploder. This will explode each list element to it's own feature. You can then use a Tester (_element_count = 0), to separate the first element.
Do you perhaps need to remove list elements except the first one? If so, there isn't a suitable transformer to do that exactly, but this combination is available. ListIndexcer (List Index: 0) AttributeRemover (Lists to Remove: MyList{}) AttributeRenamer (Old Attribute: MyList, New Attribute: MyList{0}) Of course, both the PythonCaller and the TclCaller can be also used. Python Example: -----
import fmeobjects def trimMyList(feature): myList = feature.getAttribute('MyList{}') if myList: for i in range(1, len(myList)): feature.removeAttribute('MyList{%d}' % i) -----
Tcl Example:
-----
proc trimMyList {} { for {set i 1} {[FME_AttributeExists "MyList{$i}"]} {incr i} { FME_UnsetAttributes "MyList{$i}" } } -----
I prefer Takashi's answer with Python, as it's neat and clean.
However, in a workspace test setup it worked perfectly, but in my production setup it fails. I use ListPopulator in the test setup, but I use AttributeAccumulator in my production setup.
Apparently "LIST{}" (aka MyList{}) isn't available when using Attribute Accumulator :-(
The attribute set after AttributeAccumulator contain multiple "list" entries, like this:
LIST{0}.FIRST
LIST{0}.SECOND
LIST{1}.FIRST
LIST{1}.SECOND
LIST{2}.FIRST
etc.
How must I change the Python script to accomodate for this difference ?
Mechanism is the same. ----- # Example 1 # Remove list elements except the first element. def trimList(feature): subNames = ['FIRST', 'SECOND'] for sub in subNames: subList = feature.getAttribute('LIST{}.%s' % sub) if subList: for i in range(1, len(subList)): feature.removeAttribute('LIST{%d}.%s' % (i, sub)) ----- Another approach. This works like the ListIndexer, AttributeRemover and AttributeRenamer. If there are many sub names, this might be more efficient. ----- # Example 2 # Save the first element, remove list and recover the first element. def trimList(feature): subNames = ['FIRST', 'SECOND'] for sub in subNames: value = feature.getAttribute('LIST{0}.%s' % sub) feature.removeAttribute('LIST{}.%s' % sub) feature.setAttribute('LIST{0}.%s' % sub, value) ----- Not tested. If there were typos, please correct.
I tried FME 2014 Beta which supports Null attribute value. It seems that Null cannot be set with Python / Tcl scripting. The Example 2 may not be available if the list can contain Null values.
We use 3 different kinds of cookies. You can choose which cookies you want to accept. We need basic cookies to make this site work, therefore these are the minimum you can select. Learn more about our cookies.