Skip to main content
Question

Get the attributes names with python

  • December 14, 2015
  • 9 replies
  • 524 views

danilo_fme
Evangelist
Forum|alt.badge.img+45

Hello All,

I would like to use python to get the attribute name from a feature. The API Documentation i found the Class FME Feature - getAllAttributesNames().

I my pythoncaller is:

import fme

import fmeobjects

def processFeature(feature):

atributo = getAllAttributesNames()

pass

But doenst work.

9 replies

takashi
Influencer
  • December 14, 2015

Hi,

The getAllAttributeNames method returns a list containing all attribute names which the feature has. For example, this script creates a concatenated attribute names and assign it to a new attribute. Try this and check the result to know the functionality of the method.

def processFeature(feature):
    attrNames = ','.join(feature.getAllAttributeNames())
    feature.setAttribute('attrNames', attrNames)

Takashi


danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Author
  • Evangelist
  • December 14, 2015
takashi wrote:

Hi,

The getAllAttributeNames method returns a list containing all attribute names which the feature has. For example, this script creates a concatenated attribute names and assign it to a new attribute. Try this and check the result to know the functionality of the method.

def processFeature(feature):
    attrNames = ','.join(feature.getAllAttributeNames())
    feature.setAttribute('attrNames', attrNames)

Takashi

@takashi Thanks your help. 

I used in my Workspace and its works.

Thanks!

Danilo de Lima


danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Author
  • Evangelist
  • December 14, 2015
takashi wrote:

Hi,

The getAllAttributeNames method returns a list containing all attribute names which the feature has. For example, this script creates a concatenated attribute names and assign it to a new attribute. Try this and check the result to know the functionality of the method.

def processFeature(feature):
    attrNames = ','.join(feature.getAllAttributeNames())
    feature.setAttribute('attrNames', attrNames)

Takashi

@takashi 

i have this attribute: atts_names: safe,danilo, inovacao

And i would like to remove the word danilo from my attribute atts_names? Using python

Thanks


takashi
Influencer
  • December 15, 2015
danilo_fme wrote:

@takashi 

i have this attribute: atts_names: safe,danilo, inovacao

And i would like to remove the word danilo from my attribute atts_names? Using python

Thanks

There may be various approaches. e.g.

# Use List
def processFeature(feature):
    src = feature.getAttribute('attr_names')
    if src:
        attrNames = []
        for attr in str(src).split(','):
            if attr.strip() != 'danilo':
                attrNames.append(attr)
        feature.setAttribute('attr_names'','.join(attrNames))
# Use Regular Expression
import re
def processFeature(feature):
    src = feature.getAttribute('attr_names')
    if src:
        repl = lambda m: ',' if m.group(1and m.group(2else ''
        feature.setAttribute('attr_names', re.sub(r'(,?)\s*danilo\s*(,?)', repl, str(src)))

Regarding Python language, please learn with web sites or books.

BTW, why not use the StringReplacer?

  • Attributes: attr_names
  • Text to Match: (,\s*danilo\s*|\s*danilo\s*,|\s*danilo\s*)
  • Replacement Text: <blank>
  • Use Regular Expressions: yes

"No Coding Required" (in most cases) is an advantage of FME ;)


danilo_fme
Evangelist
Forum|alt.badge.img+45
  • Author
  • Evangelist
  • December 15, 2015

Thanks again @takashi :)


Forum|alt.badge.img
takashi wrote:

Hi,

The getAllAttributeNames method returns a list containing all attribute names which the feature has. For example, this script creates a concatenated attribute names and assign it to a new attribute. Try this and check the result to know the functionality of the method.

def processFeature(feature):
    attrNames = ','.join(feature.getAllAttributeNames())
    feature.setAttribute('attrNames', attrNames)

Takashi

@tasashi

Hi,

This method doesn't seem to work for me. I am trying to get all the attribute names from my input MapInfo files from a generic "schema" reader. 

I am using FME2013 and I have also tried with the followings and nothing seemed to have worked. 

feature.getListAttribute ('attribute{}.name') - this gives me a generic out put of ['zmap_max_x', 'zmap_max_y', 'zmap_min_x', 'zmap_min_y', 'zmap_null_value']

feature.getAttribute ('attribute{}.name')

I'm new to python and hopefully this description makes sense. Thanks!


takashi
Influencer
  • May 12, 2016
judychang wrote:

@tasashi

Hi,

This method doesn't seem to work for me. I am trying to get all the attribute names from my input MapInfo files from a generic "schema" reader.

I am using FME2013 and I have also tried with the followings and nothing seemed to have worked.

feature.getListAttribute ('attribute{}.name') - this gives me a generic out put of ['zmap_max_x', 'zmap_max_y', 'zmap_min_x', 'zmap_min_y', 'zmap_null_value']

feature.getAttribute ('attribute{}.name')

I'm new to python and hopefully this description makes sense. Thanks!

Hi @judychang, FME Objects Python API has been upgrated in 2012, and the "getListAttribute" is a method in the old API. I don't remember the exact behavior of PythonCaller in FME 2013, but perhaps it still can work with the old API. See also this article: New Python FME Objects API in FME 2012


Forum|alt.badge.img
takashi wrote:

Hi @judychang, FME Objects Python API has been upgrated in 2012, and the "getListAttribute" is a method in the old API. I don't remember the exact behavior of PythonCaller in FME 2013, but perhaps it still can work with the old API. See also this article: New Python FME Objects API in FME 2012

Thanks. My getListAttribute works if I use import pyfme but not if I use import fmeobjects..


Forum|alt.badge.img

@tasashi

Hi,

This method doesn't seem to work for me. I am trying to get all the attribute names from my input MapInfo files from a generic "schema" reader.

I am using FME2013 and I have also tried with the followings and nothing seemed to have worked.

feature.getListAttribute ('attribute{}.name') - this gives me a generic out put of ['zmap_max_x', 'zmap_max_y', 'zmap_min_x', 'zmap_min_y', 'zmap_null_value']

feature.getAttribute ('attribute{}.name')

I'm new to python and hopefully this description makes sense. Thanks!


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