Skip to main content
Solved

How can I concatenate all list attributes in one go

  • January 10, 2018
  • 5 replies
  • 631 views

friedhelm
Participant
Forum|alt.badge.img+1

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)
 
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.

5 replies

denizturan1985
Participant
Forum|alt.badge.img
  • Participant
  • 11 replies
  • January 10, 2018

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+37
  • Contributor
  • 2002 replies
  • 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+56
  • Safer
  • 2554 replies
  • 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.


friedhelm
Participant
Forum|alt.badge.img+1
  • Author
  • Participant
  • 12 replies
  • 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+44
  • Influencer
  • 3427 replies
  • February 3, 2021

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!