Comments and answers for "Concatenator code" https://knowledge.safe.com/questions/82195/concatenator-code.html The latest comments and answers for the question "Concatenator code" Answer by takashi https://knowledge.safe.com/answers/82255/view.html

Alternatively, FME String functions might be helpful. e.g.

@ReplaceRegEx(@Trim(@CurrentAttribute(),","),",{2,}",",")

Fri, 09 Nov 2018 10:54:13 GMT takashi
Comment by david_r on david_r's comment https://knowledge.safe.com/comments/82239/view.html

I agree, it's really terrible. I think Safe is working on it, however...

Fri, 09 Nov 2018 08:18:38 GMT david_r
Comment by sander on sander's answer

Oh dear, this new <code> style formatting in the Knowledge Base is absolutely horrible...

Thu, 08 Nov 2018 22:46:02 GMT sander
Answer by sander

You could also use this code:

import fmeobjects def get_attributes(feature, attr_names):    for attr_name in attr_names:     value = feature.getAttribute(attr_name)      if value:        yield value def FeatureProcessor(feature):   to_concatenate = ("buildingNumber", "throughfare", "dependantthroughfare")    # Modify as needed   result = ",".join(get_attributes(feature, to_concatenate))    # Modify as needed   feature.setAttribute("CONCATENATED", result)


I'm not a big fan of list comprehensions in this particular case, because they call feature.getAttribute() twice, which is unnecessary. This is why I created the generator function get_attributes(), which only returns the attributes with a "truthy" value.

Please be aware that if the feature attribute contains the number 0 (stored as integer or float), this value is considered to be "falsy", which means that it will not be concatenated in the result. What's worse though, is that the script will choke on the str.join() call if 1 or more attributes aren't string values (which is actually what your attribute names suggest).

To tackle both problems, you could replace the get_attributes() function with this block:

def get_attributes(feature, attr_names):    for attr_name in attr_names:     value = feature.getAttribute(attr_name)      if value not in ('', None):       yield format(value)

This will work as long as you don't have any non-ASCII characters in your attributes...

Thu, 08 Nov 2018 22:44:48 GMT sander
Comment by erik_jan on erik_jan's comment https://knowledge.safe.com/comments/82212/view.html

Then create an attribute for the concatenation character, like this:

And the StringConcatenator to do the magic:

Thu, 08 Nov 2018 21:27:07 GMT erik_jan
Comment by jdh on jdh's answer

I am assuming they don't want ,, when the attribute is empty/missing.

Thu, 08 Nov 2018 21:16:38 GMT jdh
Comment by jdh on jdh's answer https://knowledge.safe.com/comments/82209/view.html

ignore the Â, that's a problem with the knowledgebase upgrade.


Put to_concatenate on a separate (indented) line from the function definition.

Either break the list comprehension line, or don't, if you do there can be nothing after it.

result = join_string.join([feature.getAttribute(attr)\ for attr in to_concatenate if feature.getAttribute(attr)])

or

result = join_string.join([feature.getAttribute(attr)for attr in to_concatenate if feature.getAttribute(attr)])

in the second case, it's all on one line.

Thu, 08 Nov 2018 21:14:55 GMT jdh
Answer by jdh

I would expect the code to work with the following formatting


import fmeobjects def FeatureProcessor(feature):   to_concatenate = ("buildingNumber", "throughfare", "dependantthroughfare")   # Modify as needed   join_string = ","   # Modify as needed   result = join_string.join([feature.getAttribute(attr)\ for attr in to_concatenate if feature.getAttribute(attr)])   feature.setAttribute("CONCATENATED", result) 



Thu, 08 Nov 2018 21:10:50 GMT jdh
Answer by erik_jan

Could this be solved by replacing the PythonCaller by a StringConcatenator transformer?

That seems so much easier.

Thu, 08 Nov 2018 21:10:46 GMT erik_jan
Comment by ingalla https://knowledge.亚搏在线safe.com/comments/82206/view.html

import fmeobjects

def FeatureProcessor(feature):to_concatenate = ("buildingNumber", "throughfare", "dependantthroughfare")

# Modify as needed

join_string = ","

# Modify as needed

result = join_string.join([feature.getAttribute(attr) \ for attr in to_concatenate if feature.getAttribute(attr)])

feature.setAttribute("CONCATENATED", result)


星期四,2018年11月08年21:08:53格林尼治时间 ingalla
Comment by takashi https://knowledge.safe.com/comments/82203/view.html

Could you please repost the script as-is, using a code block?

Thu, 08 Nov 2018 21:04:06 GMT takashi
Comment by ingalla https://knowledge.safe.com/comments/82202/view.html

Here are the errors

Python Exception <SyntaxError>: unexpected character after line continuation character (<string>, line 7)

Error executing string `import<space>fmeobjects<space><space><space><lf>def<space>FeatureProcessor<openparen>feature<closeparen>:to_concatenate<space>=<space><openparen><quote>buildingNumber<quote><comma><space><quote>throughfare<quote><comma><space><quote>dependantthroughfare<quote><closeparen><space><lf>#<space>Modify<space>as<space>needed<space><space><space><space><space><lf>join_string<space>=<space><quote><comma><quote><space><lf>#<space>Modify<space>as<space>needed<space><space><space><space><space><lf>result<space>=<space>join_string.join<openparen><openbracket>feature.getAttribute<openparen>attr<closeparen><space><backslash><space>for<space>attr<space>in<space>to_concatenate<space>if<space>feature.getAttribute<openparen>attr<closeparen><closebracket><closeparen><space><space><space><lf>feature.setAttribute<openparen><quote>CONCATENATED<quote><comma><space>result<closeparen>'

Factory proxy not initialized

PythonCaller(PythonFactory): PythonFactory failed to process feature

A fatal error has occurred. Check the logfile above for details

Thu, 08 Nov 2018 20:57:26 GMT ingalla
Comment by jdh https://knowledge.safe.com/comments/82201/view.html

Does it work if you separate the list comprehension into for and if components?

Thu, 08 Nov 2018 20:51:22 GMT jdh
Comment by takashi https://knowledge.safe.com/comments/82198/view.html

What error is logged?

Thu, 08 Nov 2018 20:38:44 GMT takashi