Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

enter image description here

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

Can anyone tell me why FindLabel is not defined?

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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)
share|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("-"))
share|improve this answer
    
The brackets are necessary for the label expression to work. See: resources.arcgis.com/en/help/main/10.1/index.html#//… –  blah238 Oct 26 '12 at 1:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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