Stacking labels with Python code in ArcGIS

6212
4
Jump to solution
03-17-2015 11:04 AM
MaeganSalinas
New Contributor

What I'm Trying To Do:

To begin, I am very, VERY, new to Python and can't wrap my head around scripting. So I need all the help I can get. Bear with me, please.

I am intending to use four fields as a stacked label for each polygon in the shapefile. I want them to look like this:

LayerGoal.JPG


What I have:

So far I have two of the four fields prepared. I wanted to make sure that I could successfully stack at least two fields before going on and finalizing the other two. Wouldn't want to do all that work just to be told I have to redo it. So in the example, I will provide the two attribute fields that I have.

CV_Ranches_Table_ArcGIS.txt.First

CV_Ranches_Table_ArcGIS.txt.Second

First representing what I want for the first line, second representing the second.

I am aware that this discussion title is only reworded a bit in the ArcGIS Help 10.2 seen here: ArcGIS Help (10.2, 10.2.1, and 10.2.2)

"Create stacked text. For example, this expression creates a label with the Name field and the two address fields all on separate lines:

"Name: " + [NAME] + '\n' + [ADDRESS_1] + '\n' + [ADDRESS_2]"

This is what I'm exactly looking for! To stack multiple attribute fields into one label. In Label Expression, I attempted to copy and paste this code into my expression dialogue window. With my code looking like:

def FindLabel ( [CV_Ranches_Table_ArcGIS.txt.First], [CV_Ranches_Table_ArcGIS.txt.Second] 😞

  return [CV_Ranches_Table_ArcGIS.txt.First] [CV_Ranches_Table_ArcGIS.txt.Second]

"Name: " + [CV_Ranches_Table_ArcGIS.txt.First] + '\n' + [CV_Ranches_Table_ArcGIS.txt.Second]

After selecting the first and second attribute fields, it automatically added the "def FindLabel" code so I kept it thinking this was standard beginning for all codes...

But I receive an error message each time.

What I'm Receiving:

Error.JPG

In the words of a very, most surely, well-known Beatles' song... "HELP. I need somebody."

Also....

Is there are a way to make multiple, individual fields on the same line? (If I decide to create different excel cells for the remainder of attributes I have yet to create. This would make it faster, I think. As far as inputting them in the spread sheet, anyway.)

Cheers!

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

It's hard to tell exactly what your final code block was (not sure if you had the important keyword 'return' in there), but this should work:

def FindLabel ( [CV_Ranches_Table_ArcGIS.txt.First], [CV_Ranches_Table_ArcGIS.txt.Second] ):
  return "Name: " + [CV_Ranches_Table_ArcGIS.txt.First] + '\n' + [CV_Ranches_Table_ArcGIS.txt.Second]

At least, that type of syntax works for simple field names, not sure about field names with '.' (honestly, I didn't know that was possible).

View solution in original post

4 Replies
XanderBakker
Esri Esteemed Contributor

It should be possible to label the attributes without the need to switch on the advanced editor:

LabelsFromCSV.png

although I would not recommend using labels based on joined text file...

To concatenate multiple field you can just add them together with for instances spaces ( + " " +) instead of using a new line (+ "\n" +).

MaeganSalinas
New Contributor

Hi Xander, I am in advanced editor.

How come you wouldn't recomment it? Do you think that is what is causing the error message? 

Thank you for the multiple field help, though.

0 Kudos
DarrenWiens2
MVP Honored Contributor

It's hard to tell exactly what your final code block was (not sure if you had the important keyword 'return' in there), but this should work:

def FindLabel ( [CV_Ranches_Table_ArcGIS.txt.First], [CV_Ranches_Table_ArcGIS.txt.Second] ):
  return "Name: " + [CV_Ranches_Table_ArcGIS.txt.First] + '\n' + [CV_Ranches_Table_ArcGIS.txt.Second]

At least, that type of syntax works for simple field names, not sure about field names with '.' (honestly, I didn't know that was possible).

MaeganSalinas
New Contributor

Awesome! It worked. Thank you so much, Darren

0 Kudos