Skip to main content
Solved

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

  • April 8, 2019
  • 2 replies
  • 145 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.

Best answer by jdh

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.

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

2 replies

jdh
Contributor
Forum|alt.badge.img+37
  • Contributor
  • 2002 replies
  • Best Answer
  • April 8, 2019

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.


  • Author
  • 1 reply
  • April 12, 2019

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 !