Skip to main content
Solved

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


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.

View original
Did this help you find an answer to your question?

2 replies

jdh
Contributor
Forum|alt.badge.img+28
  • Contributor
  • 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
  • April 12, 2019
jdh wrote:

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 !


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