How to label with multiple fields when some are empty

385
13
Jump to solution
01-11-2022 07:43 PM
LarissaBoundy
New Contributor II

Hello,

I am using ArcGIS Pro and trying to get labels in a sensible way. The layer and variables I am using to create labels can have up to five classes (Regional Ecosystems, three in the screen shot below).

LarissaBoundy_0-1641950696141.png

I have tried [$feature.RE1 +"/"+$feature.RE2+"/"+ $feature.RE3], which gives the below result, my issue being that when there was only one RE, there are is now // after the label which obviously looks silly. 

LarissaBoundy_2-1641950934108.pngLarissaBoundy_1-1641950824182.png

 Is there a way to get the extra RE labels and / to only appear if the field is not empty?

 

Thank you.

 

0 Kudos
13 Replies
IlkaIllers1
Occasional Contributor III

Well, I was foiled by my own data! Turns out it does work when you check that fields are really None (<Null>) instead of empty! This is my failsafe expression:

def FindLabel ([RE1],[RE2],[RE3]):
    a = [RE1]
    b = [RE2]
    c = [RE3]
    return "/".join([str(i) for i in [a, b, c] if i is not None and i !=""])

JohannesLindner
MVP Regular Contributor
var raw_re = [$feature.RE1, $feature.RE2, $feature.RE3, $feature.RE4, $feature.RE5]
var filled_re = []
for(var i in raw_re) {
  if(!IsEmpty(raw_re[i])) {
    Push(filled_re, raw_re[i])
  }
}
return Concatenate(filled_re, "/")

Have a great day!
Johannes
0 Kudos
MicZatorsky_AEC
Occasional Contributor II

IlkaIllers1 handling of None is important. Null requires special handling.
Null in the attribute table means None in python. 
And None plus anything equals None 

0 Kudos
LarissaBoundy
New Contributor II

Thank you everyone, Jayanta's suggestion below was successful!

(([RE1]+'/'+[RE2]+'/'+[RE3]).rstrip('/ / ')).rstrip('/')

 

0 Kudos