jeudi 5 novembre 2015

Python joining strings between address elements with an IF statement

I need to concatenate address elements (attributes) from a database using Python in FME Workbench program.These attributes will be concatenated with a comma and space or just a space. The address elements are split into 3 address lines. Multiple elements on one address line are concatenated with comma and space. The only exception is the second address line when house name (consisted from numbers and slashes or numbers only or numbers and a letter) is concatinated with a street or when house number is concatinated with a street. Here are some examples where this rule would apply:

13A Some Street
852/7 Other Street
745 Third Street

(The numbers might be house names as well)

In some cases there is other address element (MAIN_STREET_NAME) behind the street. E.g.

 1 Hill View, Hill Street

I would like to imply a rule that would add these exemption in IF statement.

1st code containing a house number is following:

import fmeobjects
def FeatureProcessor(feature):
    to_concatenate = ("HOUSE_NUMBER", "STREET_NAME", "MAIN_STREET_NAME")
    join_string = ", "
    result = join_string.join([feature.getAttribute(attr) \
        for attr in to_concatenate if feature.getAttribute(attr)])
    feature.setAttribute("ADDRESS_LINE2", result)

2nd code containing house name is similar:

import fmeobjects
def FeatureProcessor(feature):
    to_concatenate = ("HOUSE_NAME", "STREET_NAME", "MAIN_STREET_NAME")
    join_string = ", "
    result = join_string.join([feature.getAttribute(attr) \
        for attr in to_concatenate if feature.getAttribute(attr)])
    feature.setAttribute("ADDRESS_LINE2", result)

There is a general rule applied for both examples and that's join_string = ", ". I want to create two IF statements - each for one of my examples.

1st IF statement

would add a comma between main_street and street + a space only between house_number and a remaining attributes on that line.

2nd IF statement

would add a comma between main_street and street + space between house_name and a remaining attributes on that line WHEN house_name

  • begins with a number
  • might contain a slash
  • might contain a letter

If you have better ideas I would be grateful.

Aucun commentaire:

Enregistrer un commentaire