I'm trying to make an advanced label expression using the python parser and I keep getting the following error:

The expression contains an error.  
Modify the expression and try again.

Error 0 on line 0.   
Error running expression: FindLabel()   
Traceback (most recent call last):    
 File "<expression>", line 1, in <module>   
NameError: name 'FindLabel' is not defined  

enter image description here

def FindLabel ( [Direction] ):
  return  "{}° {}' {}\"".format( [Direction].split("-"))

Can anyone tell me why FindLabel is not defined?


I think there is a bug in their parser as I am not getting identical behavior between a Python prompt and the label expression evaluator.

Try the following as a workaround:

def FindLabel ( [Direction] ):
    return "{}° {}' {}".format(*[Direction].split('-')) + chr(34)

There were two problems:

  1. Due to a bug with the ESRI parser (this is legal in pure Python), you cannot escape a double-quote with a backslash if the string is surrounded by double-quotes. Similarly, you cannot escape a single-quote with a backslash if the string is surrounded by single-quotes, although this is also legal in pure Python. Triple-quoted strings also don't work the same as at a Python prompt.

    Examples that work in a Python prompt but not in a Python label expression:

    • "I am 6'2\" tall."
    • """I am 6'2" tall."""
    • 'I am 6\'2" tall.'
    • '''I am 6'2" tall.'''

    To work around this, you can instead concatenate it or format it into the string separately.

  2. The str.format() function expects args or kwargs, not a sequence. You can use the * operator to unpack a sequence into arguments, which is what I did above, and it works.

Side note -- because this is a 1-liner you can uncheck the "Advanced" box and just use the expression itself instead of a complete function:

"{}° {}' {}".format(*[Direction].split('-')) + chr(34)
| improve this answer | |
  • The only other mention I could find for this bug was in the comments of this ArcGIS Idea: Advanced Label Expressions with ArcPy – blah238 Oct 26 '12 at 2:36
  • Thanks blah238! I went with the non-advanced syntax and it worked perfectly. – Mike Oct 26 '12 at 18:37

It should actually give you a syntax error telling you your code block is invalid, because that's what's going on here.

Should look like this:

def FindLabel (Direction):
  return u"{}° {}' {}\"".format(Direction.split("-"))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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