Select to view content in your preferred language

Question regarding displaying labels using a loop in python expression in ArcMap 10.6?

1579
3
05-14-2018 03:21 AM
JoshuaPuthran2
New Contributor

Hi, 

I am looking for some assistance with regard to creating a label expression using python in ArcMap.

I have a number of point features in a map in ArcMap 10.6 that I would need to label based on information from three fields (Test1, Test2 and Test3) with numeric values. The labels from these 3 fields would have to be stacked. And each of these labels would have to appended with a date text. The format of the label would be something like this;

enter image description here

Some of the records in these fields contain zero values and so it would be required to loop through these fields and skip the zero values. That is not to have them displayed in the label.Currently I have the following scripts to work with;

-script to stack labels -This does not skip through zero values

def FindLabel ([Test1] , [Test2] , [Test3]):      return "05/10/2018" + " - " + [Test1] + "\r\n" + "05/10/2018" + " - " + [Test2] + "\r\n" + "05/10/2018" + " - " + [Test3]

-script to skip "0" values in the fields and stacks the labels, however does not append text string to each attribute value.

def FindLabel ( [Test1] , [Test2] , [Test3] ):  fields =  ([Test1] , [Test2] , [Test3])    label = ""  for field in fields:    if field == "0":      continue    else:      label = label + field + "\n"  return label

Is there a way achieve this using a python label expression in ArcMap 10.6. Any help with this would be appreciated.

0 Kudos
3 Replies
JoshuaBixby
MVP Esteemed Contributor

Looking over your second code snippet, it looks like it should work, i.e., skip 0 values and stack labels.  What are you seeing with the second code snippet?

JoshuaPuthran2
New Contributor

Hi Joshua, 

Thanks for getting back to me over this. Just wanted to provide a clarification regarding the previous post. I was able to use the second snippet to stack the labels and avoid zero values. However the date string is only appended to the first attribute field value in the iteration. Here is a screenshot of the results :

I tested this in ArcMap 10.6 as well as ArcGIS Pro 2.1.2 and received the same result. The text string is not appended to every attribute value. Only the first value is appended through the iteration.

However I was able to find a script that does provide required results;

  

def FindLabel ( [Test1],[Test2],[Test3] ):  values = [Test1],[Test2],[Test3]  date = "05/10/2018"  labellist = ["{0} - {1}".format(date, value) for value in values if value != '0']  label = "\n".join(labellist)
  return label

Using a for loop in a list comprehension does append the text string to each attribute value in the iteration.  Here is a screenshot of the results. 

JoshuaBixby
MVP Esteemed Contributor

Glad you found something that worked for you.