Hi there
I am trying to write a Python script which should change the value of an attribue based on the occurence of different substrings int the attribute. This is what i managed to create so far:
import fme
import fmeobjects
class FeatureProcessor(object):
def __init__(self):
self.featureList = []
def input(self,feature):
self.featureList.append(feature)
def close(self):
substring1 = 'blabla'
for feature in self.featureList:
if feature.getAttribute('TestAttribute') == substring1:
feature.setAttribute('TestAttribute', 'value1')
else:
feature.setAttribute('TestAttribute', 'value2')
self.pyoutput(feature)
This script can be executed but isn't doing the job right. Whith this script de Attribute value has to be exatly the same like (==) "string1".
I tried to do it like this, but the script isnt working:
def close(self):
substring1 = 'blabla1'
for feature in self.featureList:
if substring1 in feature.getAttribute('TestAttribute'):
feature.setAttribute('TestAttribute', 'value1')
else:
feature.setAttribute('TestAttribute', 'value2')
self.pyoutput(feature)
Can someone help me?
PS: I know that i could do the same thing with a conditional statement in the AttributeManager.
Edit:
The main problem was that my script was looking for strings in attribute with where, in many cases, the value was missing ("None") . With your help I managed to get the simplest solution:
def strings(feature):
Astring1 = 'test1'
Avalue1 = 'Value1'
attr = feature.getAttribute('AttributeToProcess')
if attr == None: # First it has to be checked if there is a value
feature.setAttribute('AttributeToProcess', 'NoValue')
elif Astring1 in attr:
feature.setAttribute('AttributeToProcess', Avalue)
else:
feature.setAttribute('AttributeToProcess', 'Leftover')
pass