Skip to main content
Question

How to append attribute name to an attribute value?


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

12 replies

david_r
Celebrity
  • November 15, 2017

Have you looked at the BulkAttributeRenamer?


david_r
Celebrity
  • November 15, 2017
david_r wrote:

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
  • 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
  • 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
  • November 15, 2017
dustin wrote:

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
  • 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
  • November 15, 2017
david_r wrote:
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
  • November 15, 2017
dustin wrote:

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
  • November 15, 2017
david_r wrote:

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
  • November 15, 2017
cdesisto wrote:
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
  • November 21, 2017
david_r wrote:
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
  • November 21, 2017
cdesisto wrote:
@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.

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