This worked perfect, Thank you!
How can I get this to work for a numeric wildcard? I want to exclude from labeling all values of SITE that start with 18 so I put this together:
def FindLabel ( [SITE] 😞 if int([SITE]) == '18%': return " " else: return [SITE]
The syntax checks out, but all the values that start with 18 are still labeled. If I take away the quotes around the 18 I get a syntax error. What gives? Is there no wildcard term for numbers in Python?
I think you are mixing two formats (VBscript and Python). The field SITE using the Python parser should be expressed as !SITE!.
You could use something like this with the Python parser:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">FindLabel</SPAN><SPAN class="punctuation token">(</SPAN>site<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>site<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>startswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'18'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="keyword token">else</SPAN> site<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN></SPAN>
Your expression should be:
Findlabel(!SITE!)<SPAN class="line-numbers-rows"><SPAN></SPAN></SPAN>
I tested it with this:
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> tests <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">18</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">182</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1807</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">18874</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1901</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">17</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1981</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> test <SPAN class="keyword token">in</SPAN> tests<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"in: {}, label:{}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>test<SPAN class="punctuation token">,</SPAN> FindLabel<SPAN class="punctuation token">(</SPAN>test<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">def</SPAN> <SPAN class="token function">FindLabel</SPAN><SPAN class="punctuation token">(</SPAN>site<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">return</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="keyword token">if</SPAN> str<SPAN class="punctuation token">(</SPAN>site<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>startswith<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'18'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="keyword token">else</SPAN> site <SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN> main<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
which resulted in:
in: 18, label: in: 182, label: in: 1807, label: in: 18874, label: in: 1901, label:1901 in: 17, label:17 in: 1981, label:1981<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
All values that start with 18 result in an empty string
Its weird how the in program label expression builder when set to Python still uses [XYZ] for the fields. (in ArcMap 10.3) Thanks, I wasn't aware of the startswith method, the code works now as:
def FindLabel ( [SITE] 😞 if str([SITE]).startswith('18') == True: return " " else: return [SITE]
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.