Skip to main content
Question

Replacing repetive strings


tim_bkr
Participant
Forum|alt.badge.img+5

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#"

4 replies

joepk
Influencer
Forum|alt.badge.img+20
  • Influencer
  • October 10, 2023

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.


david_r
Celebrity
  • October 10, 2023

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 


timotheebecker
Contributor
Forum|alt.badge.img+3
david_r wrote:

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.


timotheebecker
Contributor
Forum|alt.badge.img+3
joepk wrote:

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.


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