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?
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
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,
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 :)