label expression

2458
16
Jump to solution
10-06-2014 09:32 AM
SiranErysian
New Contributor

I have a attribute table for contours every 10 feet but would like to label ONLY the major contours using the value specified in the Contour field. Found some similar messages but  can't get the syntax correct. Please help. New to Python.

Tags (1)
0 Kudos
16 Replies
JamesCrandall
MVP Frequent Contributor

Your expression was actually written for VBScript, not the Python parser.  Maybe you should double-check that.

0 Kudos
SiranErysian
New Contributor

I definitely have the Python parser in the window. Here is my script and error message: What could be wrong?

0 Kudos
RichardFairhurst
MVP Honored Contributor

You are using a VB Script language Label expression.  Python has no Then with an if statement or Function/End Function syntax.  For python you have to use == to mean equals in an if statement.  A single = assigns a value only in python, which cannot be done here.  The Expression that will work for Python is:

def FindLabel ([Contour], [Index]):

  if int([Index]) == 1:

    return [Contour]

  else:

    return ""

You have to convert Index to an Int, because the Label parser automatically converts all field values to string, since only strings make valid labels.  Otherwise numeric fields and date fields would throw errors unless you explicitly converted them to string.  So you have to convert back to int or flt to make numbers behave like numbers.  Alternatively, this will work:

def FindLabel ([Contour], [Index]):

  if [Index] == '1':

    return [Contour]

  else:

    return ""

For VB Script it would be:

Function FindLabel ([Contour], [Index])

  If cint([Index]) = 1 Then

    FindLabel = [Contour]

  Else

    FindLabel = ""

  End If

End Function

0 Kudos
SiranErysian
New Contributor

I copied both of these scripts and I still am getting an error! here is a screen shot of the script and error:

I guess the previous people who were trying to help in what I thought was Python, were giving me the VB script!

Why does this still not work?

0 Kudos
RichardFairhurst
MVP Honored Contributor

It does not work because Index contains Null values and Null values cannot be converted to Int.  So you need to handle Null values (VB Script would throw the same error).

def FindLabel ([Contour], [Index]): 

  if [Index] == None:

    return ""

  elif int([Index]) == 1

    return [Contour] 

  else

    return ""

0 Kudos
SiranErysian
New Contributor

Hey,

I think I figured it out....The index field that has 1's for what I want to label had <NULL> as the value for the one's I do not want to label. Once I changed that to a 0 it labeled them like I wanted.

thanks you so much for the clarification. I will keep that script.

0 Kudos
RichardFairhurst
MVP Honored Contributor

Be sure to use my last version.  If Nulls are at all possible it is best to handle them explicitly in the expression to avoid an error rather than having to eliminate all Null values before the error will stop.  If this is a geodatabase you should set the Default value for the Index field to 0 or 1 (or -1 to act as unassigned) to avoid Null values.

0 Kudos