2

SO I am looking to do a split on a attribute that looks like 45-4. Now all the attributes have values.

here is my code so far:

import fme
import fmeobjects

def processFeature(feature):

    f_type = feature.getAttribute('F_TYP')
    if f_type:
        f_type.split('-')
        Height = f_type[0]
        Class = f_type[-1]

        feature.setAttribute("Height", Height)
        feature.setAttribute("Class", Class)

It seems to work but only gives me as an example the height = 4 and the class 4. I guess I need to take everything in front of the dash when it exists for height and everything after the dash for class. I do need the if statement to deal with no values.

4

Firstly, you're not storing the result of the .split() anywhere. Secondly, the statements which allocate values to Height and Class are actually assigning the first and last characters of the attribute string respectively (which is why your example of '45-4' results in Height and Class both being 4), rather than the two components you want. Using the code below, you should get the expected result:

import fme
import fmeobjects

def processFeature(feature):

f_type = feature.getAttribute('F_TYP')
if f_type:
    f_type_parts = f_type.split('-')
    Height = f_type_parts[0]
    Class = f_type_parts[-1]

    feature.setAttribute("Height", Height)
    feature.setAttribute("Class", Class)

Alternatively, you could remove the f_type_parts = f_type.split('-') statement, and change the assignments for Height and Class as follows:

Height = f_type.split('-')[0]
Class = f_type.split('-')[-1]
  • 1
    Thanks, I did that small change you talked about and it worked perfect. I am trying to learn Python because of how much easier it is to use for tasks like this. – Kyle K Dec 1 '15 at 15:46
  • No worries. I'm learning too, it's good fun! – Adam Dec 1 '15 at 19:43
1

If you're in FME, I'd suggest using the AttributeSplitter with - as the delimiter. Then right click on the resulting _list{} attribute shown on the AttributeSplitter OUTPUT port on the canvas, and Expose 2 elements. After that, use an AttributeRenamer to rename _list{0} to Height and _list{1} to Class.

  • how does that work with attributes with no value or mislabeled ones that do not have the dash to split them? – Kyle K Dec 1 '15 at 5:22
  • 1
    You could do ListElementCounter after the AttributeSplitter to get the number of values it split into, and then use a Tester after that to discard/complain about the ones that didn't get 2 values extracted. – Dale Lutz Dec 1 '15 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.