Skip to main content
Solved

Alter dynamic attributes

  • November 25, 2024
  • 4 replies
  • 50 views

asablomberg
Contributor
Forum|alt.badge.img+7

I am reading dynamic data and sometimes there is text fields that contain newline characters and I would like to replace them with just a space. How would I go about this? 

 

 

Best answer by nielsgerrits

I think ​@asablomberg does dynamic reading and writing, so the attributes are not know beforehand. You need something like a BulkStringReplacer. I can think of a way in FME using an AttributeExploder to explode attributes into features and an Aggregator to merge these back into single features, but this can probably be done better using Python.

 

Update: Python is still not really my thing, but I asked the AI Assist in the PythonCaller the following:

“For each feature, loop over all attributes and replace newline with space.”

It returned the following code and is works like expected.

import fme
import fmeobjects

class FeatureProcessor(object):
    def input(self, feature: fmeobjects.FMEFeature):
        for attr in feature.getAllAttributeNames():
            value = feature.getAttribute(attr)
            if isinstance(value, str):
                feature.setAttribute(attr, value.replace('\n', ' '))
        self.pyoutput(feature)

For bigger datasets, it would try and ignore attributes starting with “fme_” to maximise performance.

View original
Did this help you find an answer to your question?

4 replies

david_r
Evangelist
  • November 25, 2024

You can use the StringReplacer, with Replacement Text set to a space:

 


nielsgerrits
VIP
Forum|alt.badge.img+54
  • Best Answer
  • November 25, 2024

I think ​@asablomberg does dynamic reading and writing, so the attributes are not know beforehand. You need something like a BulkStringReplacer. I can think of a way in FME using an AttributeExploder to explode attributes into features and an Aggregator to merge these back into single features, but this can probably be done better using Python.

 

Update: Python is still not really my thing, but I asked the AI Assist in the PythonCaller the following:

“For each feature, loop over all attributes and replace newline with space.”

It returned the following code and is works like expected.

import fme
import fmeobjects

class FeatureProcessor(object):
    def input(self, feature: fmeobjects.FMEFeature):
        for attr in feature.getAllAttributeNames():
            value = feature.getAttribute(attr)
            if isinstance(value, str):
                feature.setAttribute(attr, value.replace('\n', ' '))
        self.pyoutput(feature)

For bigger datasets, it would try and ignore attributes starting with “fme_” to maximise performance.


david_r
Evangelist
  • November 25, 2024

Good catch, ​@nielsgerrits, and I agree.

However, the literal \n does not always correspond to a newline, you might also encounter \r or \r\n combined, depending on operating system. I also got this wrong in my StringReplacer screenshot above :-)

I’d do something like this:

feature.setAttribute(attr, ' '.join(value.splitlines()))

This will work for all line termination variants, independent of OS.


asablomberg
Contributor
Forum|alt.badge.img+7
  • Author
  • Contributor
  • November 25, 2024

Thanks, both of you!! Works like a charm :)


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