Skip to main content
Question

How to append attribute name to an attribute value?

  • November 15, 2017
  • 12 replies
  • 228 views

Forum|alt.badge.img

Is there a way to prefix/concatenate the attribute name and a separator character to all of the values on that feature? For example, I have a feature with 60 attributes - can I retain the values of each cell, but have the attribute name appended to the values without having to manually create each attribute.

Input:

AttributeName

abc

def

hij

Output

AttributeName

AttributeName|abc

AttributeName|def

AttributeName|hij

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.

12 replies

david_r
Celebrity
  • 8394 replies
  • November 15, 2017

Have you looked at the BulkAttributeRenamer?


david_r
Celebrity
  • 8394 replies
  • November 15, 2017

Have you looked at the BulkAttributeRenamer?

Or maybe I'm misunderstanding -- maybe "AttributeName" isn't a constant, but rather the actual name of the attribute? Example from:

 

a1=aa
b1=bb
To

 

a1|aa
b1|bb
Is that it?

Forum|alt.badge.img
  • Author
  • 5 replies
  • November 15, 2017

Apologies @david_r for not being clear. For each of the 60 columns, I would like to append the name of the attribute to every value under that attribute.


dustin
Influencer
Forum|alt.badge.img+31
  • Influencer
  • 629 replies
  • November 15, 2017

I would use the AttributeExploder to get _attr_name and _attr_value attributes on the feature, and then a StringConcatenator to get the final result of _attr_name|_attr_value.


david_r
Celebrity
  • 8394 replies
  • November 15, 2017

I would use the AttributeExploder to get _attr_name and _attr_value attributes on the feature, and then a StringConcatenator to get the final result of _attr_name|_attr_value.

That's a good way of doing it for limited datasets. Just be aware that the AttributeExploder creates a separate feature for every attribute of every feature, so if you have 10'000 features with 60 attributes each, FME has to create 600'000 features and it will be slow.

david_r
Celebrity
  • 8394 replies
  • November 15, 2017

In addition to the solution proposed by @cartoscro, here's one in Python that should have a more linear performance for larger datasets:

import fmeobjects

SEPARATOR = '|'

def processFeature(feature):
    for attr in feature.getAllAttributeNames():
        if not attr.startswith('fme_'):
            value = feature.getAttribute(attr)
            feature.setAttribute(attr, '%s%s%s' % (attr, SEPARATOR, value))

Also notice the mechanism for leaving the fme_* attributes untouched.


dustin
Influencer
Forum|alt.badge.img+31
  • Influencer
  • 629 replies
  • November 15, 2017
That's a good way of doing it for limited datasets. Just be aware that the AttributeExploder creates a separate feature for every attribute of every feature, so if you have 10'000 features with 60 attributes each, FME has to create 600'000 features and it will be slow.
Good point, @david_r

 


Forum|alt.badge.img
  • Author
  • 5 replies
  • November 15, 2017

I would use the AttributeExploder to get _attr_name and _attr_value attributes on the feature, and then a StringConcatenator to get the final result of _attr_name|_attr_value.

This was my initial attempt, but my dataset is 30million features so I was trying to avoid exploding something that size on my laptop. Thanks though!

 

 


Forum|alt.badge.img
  • Author
  • 5 replies
  • November 15, 2017

In addition to the solution proposed by @cartoscro, here's one in Python that should have a more linear performance for larger datasets:

import fmeobjects

SEPARATOR = '|'

def processFeature(feature):
    for attr in feature.getAllAttributeNames():
        if not attr.startswith('fme_'):
            value = feature.getAttribute(attr)
            feature.setAttribute(attr, '%s%s%s' % (attr, SEPARATOR, value))

Also notice the mechanism for leaving the fme_* attributes untouched.

This looks like the best option given my dataset dimensions, I will give it a go. Thanks!

 

 


david_r
Celebrity
  • 8394 replies
  • November 15, 2017
This looks like the best option given my dataset dimensions, I will give it a go. Thanks!

 

 

You're welcome. I'd be cool to know how it works out, seeing how much data you've got.

Forum|alt.badge.img
  • Author
  • 5 replies
  • November 21, 2017
You're welcome. I'd be cool to know how it works out, seeing how much data you've got.
@david_r Sorry for the delay in response. This works like a charm and the data is flowing smoothly! Thanks for the advice and the code!

 


david_r
Celebrity
  • 8394 replies
  • November 21, 2017
@david_r Sorry for the delay in response. This works like a charm and the data is flowing smoothly! Thanks for the advice and the code!

 

No worries. Glad to hear it helped you out.