5

I have a number of point features in a map in ArcMap 10.6 that I would need to label based on information form 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

  • 2
    When you evaluate the value of Test 1,2 and 3 you are testing it to see if it is equal to "0" which is a string! Assuming your fields ARE numeric fields then the test should be if field == 0: – Hornbydd May 14 '18 at 9:08
  • @Hornbydd with labelling they are all strings, type doesn't matter. – FelixIP May 14 '18 at 9:30
  • Perhaps field =='0' will work. – FelixIP May 14 '18 at 9:35
  • In your first case you add \r\n while in the second case it is only \n – JGH May 14 '18 at 10:21
  • @FelixIP never knew that! – Hornbydd May 14 '18 at 12:35
4

You can use List comprehension with an if statement and then join the list elements together to a string with newline in between:

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

enter image description here

  • Thanks BERA for getting back to me over the issue and for providing me with the code. This worked perfectly. – Joshua May 14 '18 at 19:04
  • 1
    And also for providing info on using the List Comprehension method. I have not worked with method yet. It seems like an efficient way to combine an expression, for clause and if clause to create a list. – Joshua May 14 '18 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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