Skip to main content
Solved

How can I concatenate all list attributes in one go

  • January 10, 2018
  • 5 replies
  • 401 views

Forum|alt.badge.img

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

Best answer by jdh

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)
 
View original
Did this help you find an answer to your question?

5 replies

Forum|alt.badge.img

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.


jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • Best Answer
  • January 10, 2018

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)
 

mark2atsafe
Safer
Forum|alt.badge.img+43
  • Safer
  • January 10, 2018

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.


Forum|alt.badge.img
  • Author
  • January 10, 2018

@jdh

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

Thanks to the others as well-


ebygomm
Influencer
Forum|alt.badge.img+31
  • Influencer
  • February 3, 2021
mark2atsafe wrote:

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


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