2

Can't get advanced python label expression to work. I am trying to get my if else expression to either concatenate or not based on if two fields are blank or not.

If SUB_UNIT_TYPE and SUB_UNIT_ID are not null then concatenate them together on a new line with a space between the values from the two fields. I can't get python to find the null value.

I tried all the suggestions from this post. Using if/elif for passing null values to Python of label expression in ArcGIS for Desktop?

def FindLabel ( [Add_Num], [PROP_UNIT_TYPE], [PROP_UNIT_ID],[Sub_Unit_Type],[Sub_Unit_ID]):
   if (not([SUB_UNIT_TYPE] is None) and not([SUB_UNIT_ID] is None)):
       L = ([Add_Num] +  '\n' + [Sub_Unit_Type] + [Sub_Unit_ID])
   else:
      L = ([Add_Num])
   return L

Tried this. Still need to add a space between the four subaddress fields and write an elif statement for if the PROP_UNIT_TYPE and PROP_UNIT_ID is null but the SUB_UNIT_TYPE and SUB_UNIT_ID fields are not null. Is this really the best way to go? Its easier in C# and SQL Server but python is pretty much the only language accepted in labeling expressions.

def FindLabel ( [Add_Num], [PROP_UNIT_TYPE], [PROP_UNIT_ID], [Sub_Unit_Type], [Sub_Unit_ID]):
   if ([PROP_UNIT_TYPE] is None and [PROP_UNIT_ID] is None and [SUB_UNIT_TYPE] is None and [SUB_UNIT_ID] is None):
      L = ([Add_Num])
   else:
      L = '{0}\n{1}{2}\n{3}{4}'.format([Add_Num],[PROP_UNIT_TYPE],[PROP_UNIT_ID],[Sub_Unit_Type],[Sub_Unit_ID])
   return L

closed as off-topic by PolyGeo Jun 4 '16 at 2:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    the L in the if is at a different indent to the L in else – Midavalo Jun 2 '16 at 21:38
  • I think Add_Num or Sub_Unit_ID is a numeric object and you can't concatenate numbers and strings.. try '{0}\n{1} {2}'.format([Add_Num],[Sub_Unit_Type],[Sub_Unit_ID]) it's a little more robust for types. Nice catch @Midavalo, I didn't even see that until you pointed it out.. this is why I TAB indent, spaces are so easy to get wrong by 1 and not even see. – Michael Stimson Jun 2 '16 at 21:39
  • Not sure @MichaelMiles-Stimson I think on label expression all variables get cast as strings. – PolyGeo Jun 2 '16 at 21:42
  • 1
    Interesting @PolyGeo, I'll try that out. I always assumed that the variable types were the field types.. not that types are all that rigid in python with its 'duck type' variables. Update: you are absolutely correct, return '{0} {1}'.format(type( [File]), type([FID])) returns <type 'unicode'> <type 'unicode'> so variables are converted to strings for the purposes of label expressions... well I've learned a new thing today! This just proves that assumptions aren't always correct. – Michael Stimson Jun 2 '16 at 21:44
  • 1
    @MichaelMiles-Stimson "Field values are automatically cast to text strings" from resources.arcgis.com/en/help/main/10.1/index.html#//… – PolyGeo Jun 2 '16 at 21:48