Skip to main content

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? 

 

 

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

 


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.


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.


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


Reply