Question

How to replace multiple strings in single attribute?

  • 7 January 2020
  • 6 replies
  • 16 views

Badge

I have attribute, that contains mostly 3 street names (there are 1 to 4 street names per attribute).

Street names are in finnish, but I would need them also in swedish. I have csv-file that contains street names in finnish and swedish

Single street name would be easily changed with AttributeValueMapper, but how can I change 3 in same attribute?

for example "Mikael Lybeckin tie (Kasavuorentie-Bredantie)" => "Mikael Lybecks vägen (Kasabergsvägen-Bredavägen)"

This solution is quite close ebygomm Python script


6 replies

Userlevel 4

Try something like this in a PythonCaller (assuming Python 3):

import fmeobjects
import csv

class multi_string_replace(object):

    def __init__(self):
        with open(r'C:\my_streets\street_mapping.csv') as csvfile:
            self.street_mappings = dict(csv.reader(csvfile, delimiter=';'))

    def input(self, feature):
        street_name = feature.getAttribute('street_name_attribute')
        if street_name:
            for search_name, replace_name in self.street_mappings.items():
                street_name = street_name.replace(search_name, replace_name)
            feature.setAttribute('street_name_attribute', street_name)
        self.pyoutput(feature)

This will read the file 'C:\my_streets\street_mapping.csv' once at startup, caching all the search-replace pairs in a Python dictionary. For each feature that enters the PythonCaller, the attribute street_name_attribute will be processed to replace all the names found in the street name attribute with the corresponding values from the csv.

Badge

Try something like this in a PythonCaller (assuming Python 3):

import fmeobjects
import csv

class multi_string_replace(object):

    def __init__(self):
        with open(r'C:\my_streets\street_mapping.csv') as csvfile:
            self.street_mappings = dict(csv.reader(csvfile, delimiter=';'))

    def input(self, feature):
        street_name = feature.getAttribute('street_name_attribute')
        if street_name:
            for search_name, replace_name in self.street_mappings.items():
                street_name = street_name.replace(search_name, replace_name)
            feature.setAttribute('street_name_attribute', street_name)
        self.pyoutput(feature)

This will read the file 'C:\my_streets\street_mapping.csv' once at startup, caching all the search-replace pairs in a Python dictionary. For each feature that enters the PythonCaller, the attribute street_name_attribute will be processed to replace all the names found in the street name attribute with the corresponding values from the csv.

Thank you @david_r! I am using Python 3.7.

I changed csv-filename and attribute name, but I get error and I don't know why

0684Q00000ArMXtQAN.png

Userlevel 4

Thank you @david_r! I am using Python 3.7.

I changed csv-filename and attribute name, but I get error and I don't know why

The code above was untested and contained two errors, I've fixed them now. Hopefully it works as promised.

Userlevel 3
Badge +18

Thank you @david_r! I am using Python 3.7.

I changed csv-filename and attribute name, but I get error and I don't know why

hi @bazooka, i had this error before with using wrong indentations... Everything looked nice, but i used a mix of spaces and tabs: not OK.... Under options you can enable 'Show spaces/tabs' (and line numbers too by the way).

Maybe worth checking this out first?

Userlevel 4

hi @bazooka, i had this error before with using wrong indentations... Everything looked nice, but i used a mix of spaces and tabs: not OK.... Under options you can enable 'Show spaces/tabs' (and line numbers too by the way).

Maybe worth checking this out first?

I had forgotten a closing parenthesis on line 8, unfortunately... Fixed now.

But yes indeed, indentations are a common issue when copy/pasting code, in particular from forums like these.

Badge

I had forgotten a closing parenthesis on line 8, unfortunately... Fixed now.

But yes indeed, indentations are a common issue when copy/pasting code, in particular from forums like these.

Now code works! Thanks again!

Reply