Creating multiple commands within Label Expression - Python

3956
5
Jump to solution
03-19-2015 04:05 PM
MaeganSalinas
New Contributor

Hello,

I am trying to create a Python expression for my labels to create a set of multiple lines displaying various field of my attribute table (accomplished), and to make sure that one of the fields that I include has a command to only capitalize the first letter of the word.

This is what I have:

def FindLabel ( [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_N] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_M] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.VITICULTUR] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_PLANT] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.ACRES] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.VineRow] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.TRELLIS] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.DIRECT] , [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.RTSTLK]  😞
   return " " + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_N] + ' ' +  '-' + ' ' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_M] + '\n' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.VITICULTUR] + ' ' + '-' + ' ' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.PBLK_PLANT] +  '\n' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.ACRES] + ' ' + 'acres' + ' ' + '-' + ' ' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.VineRow] +  '\n' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.TRELLIS] + ' ' + '-' + ' ' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.DIRECT] + ' ' + '-' + ' ' + [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.RTSTLK]

   S = [CentralValleyRanches_AllInfo_woCOORD_REDO.txt.VITICULTUR]
   S = S.title()
   return S

ex.JPG

The expression successfully created this. However, I want to make the "MERLOT" in the label into "Merlot"

My questions are:

1. How do you add multiple commands within an expression?

2. Is there something you must include each time there is a new command?

Many thanks in advance!

0 Kudos
1 Solution

Accepted Solutions
TedKowal
Occasional Contributor III

I am a VB guy learning Python..... so take that in stride.

It will help if you used the editors code formatter to format your code.  Python indentations are critically important.  Looking at your code...the proper indentation is lost so I have no clue on what your doing....

I take it that you want to do multiple things at once ....

def FindLabel([Field1],[Field2]....[FieldN]):
     myLabel = ""
     myLabel = [Field1] + " " + [Field2] + "\n"
     myLabel = myLabel + [Field3].title() + "\n"
     s = [Field5]
     if [Field4] = "Something": #Capitalize first letter
          myLabel = myLabel + s.capitalize()
     else: #UpperCase the word(s)
          myLabel = myLabel + s.upper()
     return myLabel
     

Hope this helps somewhat....

View solution in original post

5 Replies
TedKowal
Occasional Contributor III

I am a VB guy learning Python..... so take that in stride.

It will help if you used the editors code formatter to format your code.  Python indentations are critically important.  Looking at your code...the proper indentation is lost so I have no clue on what your doing....

I take it that you want to do multiple things at once ....

def FindLabel([Field1],[Field2]....[FieldN]):
     myLabel = ""
     myLabel = [Field1] + " " + [Field2] + "\n"
     myLabel = myLabel + [Field3].title() + "\n"
     s = [Field5]
     if [Field4] = "Something": #Capitalize first letter
          myLabel = myLabel + s.capitalize()
     else: #UpperCase the word(s)
          myLabel = myLabel + s.upper()
     return myLabel
     

Hope this helps somewhat....

MaeganSalinas
New Contributor

I've read that the label expression python feature in ArcGIS does not require the same indentations as it does elsewhere. My code with those indentations of just two (like you see in the one I've pasted) works perfectly fine, up to the point of wanting to capitalize that specific attribute. Clearly the latter would not work because I simply just had not any clue of what to type

Thank you for your help.

0 Kudos
RichardFairhurst
MVP Honored Contributor

You do not need to concatenate every character or word separately in a string.  You wrote:

+ ' ' + 'acres' + ' ' + '-' + ' ' +

You should have written:

+ ' acres - ' +

Or you wrote:

+ ' ' +  '-' + ' ' +

You should have written:

+ ' - ' +

MaeganSalinas
New Contributor

Ha! Oops. The little things one doesn't realize. That will make it so much cleaner. Thank you, Richard!

0 Kudos
MaeganSalinas
New Contributor

So to sum this up, with the help of Ted Kowal, add .title() at the end of your attribute that you wish to properly capitalize.

Easy

0 Kudos