Solved

Capitalize each word except articles and join anew the splitted strings with Python ?

  • 8 April 2019
  • 2 replies
  • 17 views

Hi everyone,

I'm quite new to this community so I apologize in advance if I make some mistake in the introduction of my question.

I have a column with building names (dromore lodge, killowen house, church of jesus etc.) and I want to use PythonCaller to capitalize each word except some of them like the articles (a, an, in, of...). 

I've tried this simple code with poor results :

import fme
import fmeobjects
import re 

def processFeature(feature):
    str=feature.getAttribute('BUILDING_NAME')
    exceptions = ['a', 'an', 'of', 'the', 'is']
    
    for word in re.split(' ',str):
        if word not in exceptions:
            word = word.capitalize()
    
    feature.setAttribute("B_NameCap", word)
 
    pass

What I have as a result is the last word capitalize as I want (Lodge, House etc.) in the column I've created (B_NameCap). 

I understand Python is giving me the last result overwriting the first one in the list.

I've tried to .append a new variable with the capitalized data and .join the data anew to no avail. I do not succeed to get around this.

I can split my data, capitalize it someway but I can't work a way to get it back as I would like (Dromore Lodge, Killowen House, Church of Jesus)

 

Thank you for your help.

icon

Best answer by jdh 8 April 2019, 23:49

View original

2 replies

Badge +22

You need to rebuild your words until the full string before setting it as an attribute.

Something like

import fme
import fmeobjects
import re

def processFeature(feature):
   str=feature.getAttribute('BUILDING_NAME')
 exceptions = ['a', 'an', 'of', 'the', 'is']     strList =[]
   for word in re.split(' ',str):
        if word not in exceptions:
            word = word.capitalize()
        strList.append(word)
   feature.setAttribute("B_NameCap", " ".join(strList))
 

Though if I were writing it from scratch, I would probably use str.title() to capitalize every word, and then run a regex to fix the exceptions.

You need to rebuild your words until the full string before setting it as an attribute.

Something like

import fme
import fmeobjects
import re

def processFeature(feature):
   str=feature.getAttribute('BUILDING_NAME')
 exceptions = ['a', 'an', 'of', 'the', 'is']     strList =[]
   for word in re.split(' ',str):
        if word not in exceptions:
            word = word.capitalize()
        strList.append(word)
   feature.setAttribute("B_NameCap", " ".join(strList))
 

Though if I were writing it from scratch, I would probably use str.title() to capitalize every word, and then run a regex to fix the exceptions.

I knew that I needed to append and join but didn't know how to write it.

I even try this to no avail :

def processFeature(feature):

 

    str=feature.getAttribute('BUILDING_NAME')

 

    final = []

 

    exceptions = ['a', 'an', 'of', 'the', 'is']

 

 

 

    for word in re.split(' ',str):

 

        if word not in exceptions:

 

            word = word.capitalize()      

 

        final.append(word)

 

    return ' '.join(final)

 

 

 

    feature.setAttribute("B_NameCap", final)

 

 

 

    pass

 

Looking at your code, I was close but not quite there. Anyway, thank you for the correction !

Reply