Solved

How can I concatenate all list attributes in one go

  • 10 January 2018
  • 5 replies
  • 68 views

Badge

I know how to use ListConcatenator in order to concatenate all list values into a single attribute. However, what can I do if my feature has multiple list attributes? I want to concatenate all values of all lists into respective single attributes. The resulting single attributes should have the names of the original list. I don' know the names of the lists in advance.

Examples:

colour{0} = 2

 

colour{1} = 3

 

colour{2} = 2

single attribute colour = 2,3,2

shape{0} = triangle

 

shape{1} = square

 

shape{2} = circle

single attribute shape = triangle,square,circle

Thanks for any help

icon

Best answer by jdh 10 January 2018, 20:57

View original

5 replies

Badge

I guess you can use Attribute Manager for this. Create new Output Attribute value as "single attribute sahpe" and add in "Attribute Value" like below.

@Value(shape{0}) @Value(shape{1}) @Value(shape{2})

I hope it will work.

Badge +22

In a dynamic scenario, where you don't know the names of the lists, I can't think of a way to do this other than via scripting.

Something along the lines of

import fme
import fmeobjects
import re

def processFeature(feature):
    # get all the unique list names
    rootNames =set()
    attrList= feature.getAllAttributeNames()
    for attr in attrList:
        list = re.search('(.*){[0-9]*}(.*)', attr)
        if list:
            listName = list.group(1)+"{}"+list.group(2)
            rootNames.add(listName)
   
    #get the values for each unique list and concatenate them
    for listAttr in rootNames:
        listVal = feature.getAttribute(listAttr)
        concatVal = ','.join(listVal)
        newAttr = listAttr.replace('{}','')
        
        #add the attribute to the feature
        feature.setAttribute(newAttr, concatVal)
 
Userlevel 4
Badge +25

There is an existing enhancement request for this behaviour. It's PR#15122 - the low number tells you how old that request is. Sadly I don't think there have been a lot of requests. But I will add a link to this thread and see if I can increase the priority.

Badge

@jdh

Great - your python script does the trick. Thanks a lot!!!!!

Thanks to the others as well-

Userlevel 1
Badge +21

There is an existing enhancement request for this behaviour. It's PR#15122 - the low number tells you how old that request is. Sadly I don't think there have been a lot of requests. But I will add a link to this thread and see if I can increase the priority.

Arrived here this morning after thinking there must be an alternative to many listconcatenators or a python script but seems not, I thought it was just lack of coffee and my brain not being awake!

Reply