<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: label expression: 'if not field 1, then field 2', how is this achieved in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322532#M25128</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;thank you for your response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;yes I am trying to do this in ArcMap session, using the label expression maker. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Your previous suggestion does not seem to work as I cannot add in carriage returns?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;is there anything else i can try?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;JB&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 19 Mar 2014 01:41:16 GMT</pubDate>
    <dc:creator>JennyBiddlecombe</dc:creator>
    <dc:date>2014-03-19T01:41:16Z</dc:date>
    <item>
      <title>label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322529#M25125</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am looking to create a label expression that will result in the following: display 'field 1', however if 'field 1' is blank, display 'field 2'&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am sure this can be written a number of ways to get the same result (eg other way round: display 'field 2' unless 'field 1' is not blank)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Would anyone be able to show how to do this?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thank you very much- any help would be greatly appreciated. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;JB&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 18 Mar 2014 20:44:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322529#M25125</guid>
      <dc:creator>JennyBiddlecombe</dc:creator>
      <dc:date>2014-03-18T20:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322530#M25126</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Are you trying to label an expression from with an arcpy script or as a Label Expression inside an ArcMap session?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you're doing this within ArcMap, you'd need to use the Advanced Python Label Expression window and use a function something like this:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel ( [field 1] , [field 2] ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if [field1] != "":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [field1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; return [field2]&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:19:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322530#M25126</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-11T15:19:00Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322531#M25127</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you have Null values, I'd recommend replacing them with something like an empty string "", or some other filler character.&amp;nbsp; I've never gotten Null values to play nicely with label expressions with Python or VBScript.&amp;nbsp; Then something like this works:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
def FindLabel ( [FIELD1], [FIELD2] ):
&amp;nbsp; if [FIELD1] == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp; return [FIELD2]
&amp;nbsp; elif [FIELD2] == "":
&amp;nbsp;&amp;nbsp;&amp;nbsp; return [FIELD1]
&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; return ""&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Edit: Scooped!&amp;nbsp; Matt's code is tidier, too.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:19:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322531#M25127</guid>
      <dc:creator>KerryAlley</dc:creator>
      <dc:date>2021-12-11T15:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322532#M25128</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi, &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;thank you for your response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;yes I am trying to do this in ArcMap session, using the label expression maker. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Your previous suggestion does not seem to work as I cannot add in carriage returns?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;is there anything else i can try?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;JB&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Mar 2014 01:41:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322532#M25128</guid>
      <dc:creator>JennyBiddlecombe</dc:creator>
      <dc:date>2014-03-19T01:41:16Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322533#M25129</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;"A label expression is limited to a single line of code unless you check the Advanced box on the Label Expression dialog box."&amp;nbsp; (From the &lt;/SPAN&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00s800000027000000"&gt;Building Label Expressions&lt;/A&gt;&lt;SPAN&gt; help page) &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;good luck!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Mar 2014 02:14:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322533#M25129</guid>
      <dc:creator>KerryAlley</dc:creator>
      <dc:date>2014-03-19T02:14:33Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322534#M25130</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi there,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;this results in only displaying field 2...&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Any other suggestions? &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;thank you very much for your help!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;JB&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Are you trying to label an expression from with an arcpy script or as a Label Expression inside an ArcMap session?&lt;BR /&gt;&lt;BR /&gt;If you're doing this within ArcMap, you'd need to use the Advanced Python Label Expression window and use a function something like this:&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def FindLabel ( [field 1] , [field 2] ):
&amp;nbsp;&amp;nbsp;&amp;nbsp; if [field1] != "":
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return [field1]
&amp;nbsp;&amp;nbsp;&amp;nbsp; return [field2]&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:19:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322534#M25130</guid>
      <dc:creator>JennyBiddlecombe</dc:creator>
      <dc:date>2021-12-11T15:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322535#M25131</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Hi there,&lt;BR /&gt;this results in only displaying field 2...&lt;BR /&gt;Any other suggestions? &lt;BR /&gt;thank you very much for your help!&lt;BR /&gt;JB&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;What are the empty field values in your table?&amp;nbsp; Are they &amp;lt;Null&amp;gt;?&amp;nbsp; Spaces?&amp;nbsp; Empty?&amp;nbsp; If you're not absolutely sure, you can use a definition query builder to view the Unique Values.&amp;nbsp; The Show Values button on the Label Expression window has a view that I find less informative.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm guessing you don't have empty fields ("") because that is the condition for displaying field 1 in Matt's code.&amp;nbsp; You might find (for example) that you have Null values, single spaces (" "), double spaces ("&amp;nbsp; "), or possibly a mixture of empty-looking values.&amp;nbsp; (Note that there should be two spaces between the last pair of quotes... the text formatting stripped my second space!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 19 Mar 2014 13:21:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322535#M25131</guid>
      <dc:creator>KerryAlley</dc:creator>
      <dc:date>2014-03-19T13:21:41Z</dc:date>
    </item>
    <item>
      <title>Re: label expression: 'if not field 1, then field 2', how is this achieved</title>
      <link>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322536#M25132</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In Python I use None to check for null values. Try something like:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
if [field1].strip() != "" AND NOT [field1] Is None:

or maybe you could use:

if len([field1].strip()) &amp;gt; 0:
 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Not sure what your values are, so see what works best. This is also assuming field1 is a string, not a number.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:19:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/label-expression-if-not-field-1-then-field-2-how/m-p/322536#M25132</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T15:19:07Z</dc:date>
    </item>
  </channel>
</rss>

