0

Looking to add a function of my labeling expression to not label feature with the word 'TO". For example, some roads are named '5th TO I-10'. I am using Python as the parser with advanced checked.

Here is what I have so far. The split function is working as intended.


def FindLabel ([SFEANME]):
   word = 'TO'
   if word in [SFEANME]
      return Null
else
    return [SFEANME].split(" ")[0]

[SFEANME] is the field for the feature name and is text type.

Here is the error I get when verifying: "The expression contains an error. Modify the expression and try again. Error 0 on line 0. SyntaxError: invalid syntax (, line 3)."

I have tried using "" and without "" around TO, as well as simply: if TO in [SFEANME]

I have not been able to find anyone trying to do something like this in Label Expression so maybe there is a better way, or I do not know how to syntax properly like the error message says.

1 Answer 1

2

There are a number of problems in your script:

  1. Missing colon (:) at end of if line
  2. Null is not a keyword in Python; use None for NULL in Python
  3. Missing colon (:) at end of else line
  4. Inconsistent use of leading whitespace causing poor indentation (Python is very fussy about this... note that I only picked this up when doing a copy/paste, so it may not actually be true in your real code, but is certainly true in the code you have here)

I think the error is complaining about the first of these items.

Try this code, which fixes the problems listed above:

def FindLabel ([SFEANME]):
    word = 'TO'
    if word in [SFEANME]:
        return None
    else:
        return [SFEANME].split(" ")[0]
2
  • Thank you for the help. That works! I found that I can also filter out all labels that would have 'TO' with SQL Query when Method is set to define classes of features and label each class differently with NOT LIKE '%TO%'.
    – AFang
    Commented Mar 25 at 13:27
  • @AFang yes, that's probably a better way to do it. :-) Commented Mar 28 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.