How to label with multiple fields when some are empty

318
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
1 Solution

Accepted Solutions
JayantaPoddar
MVP Esteemed Contributor

alright. Check this one (Python)

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

 

or

([RE1]+'/'+[RE2]+'/'+[RE3]).replace('/ /','')


Think Location

View solution in original post

0 Kudos
13 Replies
JayantaPoddar
MVP Esteemed Contributor

Could you use Python Language?

Could you check if rstrip('/') function works for you?

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

 



Think Location
0 Kudos
LarissaBoundy
New Contributor II

Hi Jayanta,

Thanks for your comment. I just tried that, got an error message, and ($feature.RE1 +"/"+$feature.RE2+"/"+ $feature.RE3).rstrip('/')

For both I got the message 

LarissaBoundy_0-1641963590456.png

Thank you.

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

Copy-paste my script if you have changed the language to Python.

No need to use $feature. Also call the fields from the Fields list.

Please share screenshot of your label expression as well.



Think Location
0 Kudos
LarissaBoundy
New Contributor II

Thank you, I hadn't changed language to Python, but still did not work

LarissaBoundy_0-1641968456352.png

 

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

alright. Check this one (Python)

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

 

or

([RE1]+'/'+[RE2]+'/'+[RE3]).replace('/ /','')


Think Location
0 Kudos
LarissaBoundy
New Contributor II

Thank you so much you legend, the first one was successful. 

0 Kudos
HuubZwart
Occasional Contributor

Not the most elegant solution, but you could employ the removeEmpty parameter from Split (Arcade). I.e. concatenate the values using Concatenate, split the new string with the removeEmpty parameter set and then concatenate again to get your label. 

0 Kudos
IlkaIllers1
Occasional Contributor III

I use something like this in the advanced expression of Python:

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])

0 Kudos
IlkaIllers1
Occasional Contributor III

Sorry, I just tried it and realised it doesn't work with "/". It still has the unwanted extra //. Curious! The same code with ", " instead works fine for me. I don't know why, sorry!

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])

0 Kudos