Solved

Multiple parameter in PythonCaller script


Hi,

I'm trying to make a script that will replace/erase some of words from table of strings.

I want to create list of key words in a Published Parameter (multiple choice) that will be piked by user. When I pick one KeyWord script works fine but with more doesn't work at all. How can I make it to loop through all picked words?

str1 is with Published Parameter and

str2 is with list of words, but there should be more of them so it will look horrible ;)

EDIT: words are single. Additional problem I have now is how to make them be whole words only eg. Landhotel shouldn't be erased HOTEL, only Land Hotel -> Land.

And additional question is there a way to replace RegEx matching with sth shorter? I'm new in scripting so my code is vary basic :)

I have such a script in PythonCaller:

import fme
import fmeobjects
import re

def KeyWord_replacer (feature):

    str1 = feature.getAttribute('Name')
    str2 = feature.getAttribute('Name2')
    keyW = FME_MacroValues['KEY_WORDS']

    str1 = str1.lower()
    str2 = str2.lower()
    keyW = keyW.lower()
    
    str1 = str1.replace(keyW,'')
    str2 = str2.replace('restaurant','').replace('ristorante','').replace('hotels','').replace('hotel','')


    str1 = re.sub(r'\s{2,}',' ',str1)
    str2 = re.sub(r'\s{2,}',' ',str2)
    
    str1 = re.sub(r'\A\s+','',str1)
    str2 = re.sub(r'\A\s+','',str2)
    
    str1 = re.sub(r'\s$','',str1)
    str2 = re.sub(r'\s$','',str2)


    feature.setAttribute('Name', str1)
    feature.setAttribute('Name2', str2)
icon

Best answer by david_r 20 June 2018, 09:57

View original

8 replies

Userlevel 4

The published parameter is basically a long string, which you'll have to split up and iterate over. For example:

keyWords = FME_MacroValues['KEY_WORDS'].split(' ')
 
for keyW in keyWords:
        keyW = keyW.lower()
        ... do whatever is needed with each keyword here ...

Badge

Hi 

Looking at the output of multiple choice parameters, the answer will depend on if your keywords are all single words, all many words or a mix.

As the parameters comes back as 

Ham Spam "Ham and Eggs"

If its all single words then its just a simple matter of:

keywordList = FME_MacroValues['KEY_WORDS'].split(" ")
for keyW in keywordList:
#rest of code here

In regards to re I don't really use that so unable to help there!

The published parameter is basically a long string, which you'll have to split up and iterate over. For example:

keyWords = FME_MacroValues['KEY_WORDS'].split(' ')
 
for keyW in keyWords:
        keyW = keyW.lower()
        ... do whatever is needed with each keyword here ...

Thank you very much! I just don't have basics :(

 

 

Userlevel 4
Thank you very much! I just don't have basics :(

 

 

No worries, we all had to start somewhere
Userlevel 4

If you have multi-word values, such as the example posted by @davidrich, you can use the shlex module to split the published parameter text into its individual components, e.g.

import shlex

parameter = 'Ham Spam "Ham and Eggs"'
keyWords = shlex.split(parameter)
# keyWords = ['Ham', 'Spam', 'Ham and Eggs']

If you have multi-word values, such as the example posted by @davidrich, you can use the shlex module to split the published parameter text into its individual components, e.g.

import shlex

parameter = 'Ham Spam "Ham and Eggs"'
keyWords = shlex.split(parameter)
# keyWords = ['Ham', 'Spam', 'Ham and Eggs']
I have only single words. But how to avoid replacing part of words like in EDIT note above.
Userlevel 4

If you need to constrain the replacements to word boundaries, you can again use the shlex module to your advantage, e.g.

import shlex

words_to_remove = shlex.split('HOTEL RESTAURANT RISTORANTE')

test_string1 = 'Landhotel La Paix'
test_string2 = 'Land hotel La Paix'

print ' '.join([word for word in shlex.split(test_string1) if word.upper() not in words_to_remove])
# -> 'Landhotel La Paix'

print ' '.join([word for word in shlex.split(test_string2) if word.upper() not in words_to_remove])
# -> 'Land La Paix'

If you need to constrain the replacements to word boundaries, you can again use the shlex module to your advantage, e.g.

import shlex

words_to_remove = shlex.split('HOTEL RESTAURANT RISTORANTE')

test_string1 = 'Landhotel La Paix'
test_string2 = 'Land hotel La Paix'

print ' '.join([word for word in shlex.split(test_string1) if word.upper() not in words_to_remove])
# -> 'Landhotel La Paix'

print ' '.join([word for word in shlex.split(test_string2) if word.upper() not in words_to_remove])
# -> 'Land La Paix'

Now I have problem with words that has apostrophe, because it causes error : No closing quotation :(

 

 

Reply