Skip to main content

I have string attributes which contain repetitions of the type ";;;". How to get rid of these, however numerous the ";" are. I also want to nullify the attribute if it ends up being ";" but that can be done with a trimmer afterwards.

 

I also would like to delete other repetitions.

Examples

"31;31;44" should become "31;44"

"*3*;*3*;*4*" or "*3*;*4*;*3*" should become "*3*;*4*"

 

"#TH#;#BAT#;#TH#;#BAT#" should become "#TH#;#BAT#"

I think a PythonCaller would be best for this. You can convert your string to list oldlist using split(';'), create an empty list newlist, loop over your oldlist and append all the elements which are not yet in your newlist to that newlist.

Using join() you can convert your newlist back to a string.


You could also accomplish the same thing using regular transformers (no Python):

  1. AttributeSplitter on ; and set "Drop emtpy parts" to yes
  2. ListDuplicateRemover on the resulting list attribute
  3. ListConcatenator to join the remaining list elements together with ;

If you have a lot of further processing downstream from this, you will want to use e.g. an AttributeManager to remove the temporary attributes such as the parts list before continuing

If you want to go the Python route, here's an example:

import fmeobjects
 
def RemoveDuplicates(feature):
    value = feature.getAttribute('value') or ''
    parts = value.split(';')
    unique_parts = set(parts)
    joined_parts = ';'.join(unique_parts)
    feature.setAttribute('new_value', joined_parts)

It will assume an incoming attribute "value" and the result will be output as "new_value". Result:

image 


You could also accomplish the same thing using regular transformers (no Python):

  1. AttributeSplitter on ; and set "Drop emtpy parts" to yes
  2. ListDuplicateRemover on the resulting list attribute
  3. ListConcatenator to join the remaining list elements together with ;

If you have a lot of further processing downstream from this, you will want to use e.g. an AttributeManager to remove the temporary attributes such as the parts list before continuing

If you want to go the Python route, here's an example:

import fmeobjects
 
def RemoveDuplicates(feature):
    value = feature.getAttribute('value') or ''
    parts = value.split(';')
    unique_parts = set(parts)
    joined_parts = ';'.join(unique_parts)
    feature.setAttribute('new_value', joined_parts)

It will assume an incoming attribute "value" and the result will be output as "new_value". Result:

image 

Thanks. That worked fine for me.


I think a PythonCaller would be best for this. You can convert your string to list oldlist using split(';'), create an empty list newlist, loop over your oldlist and append all the elements which are not yet in your newlist to that newlist.

Using join() you can convert your newlist back to a string.

Nice too.


Reply