How do I remove null values from Maplex Labeling using Python?

1270
11
03-01-2017 11:10 AM
MikeHutchison
New Contributor

def FindLabel ( [INSERTEDMAINSIZE], [INSERTEDMAINMATERIAL] :(
  return str([INSERTEDMAINSIZE]) + " " + str([INSERTEDMAINMATERIAL])


I'm using ArcMap 10.2.  There are null values in [INSERTEDMAINSIZE] and [INSERTEDMAINMATERIAL].  Instead of labeling the Null values as "None" I don't want them to show up on the map at all.  If there is a value for that features, I want that to label, just not the null values.  How do I add this to the script?

Thanks

0 Kudos
11 Replies
DarrenWiens2
MVP Honored Contributor

Similar to here: join 4 strings to the one if they are not empty in python - Stack Overflow 

return ' '.join(filter(None, ([INSERTEDMAINSIZE],[INSERTEDMAINMATERIAL])))
MikeHutchison
New Contributor

That worked great.  Now I have an additional problem, how do I include this in my overall script to annotate a feature using other fields, not just the ones above, that don't have null values.  This is my entire script.  Where would I add in what you sent me.

def FindLabel ( [MAINSIZE], [MAINMATERIALTYPE], [SYSTEMMAOP], [INSTALLATIONDATE], [WORKORDERNUMBER], [INSERTEDMAINSIZE], [INSERTEDMAINMATERIAL] :(
  return [MAINSIZE] +  "  "  +  [MAINMATERIALTYPE] +  "  "  +  [SYSTEMMAOP] + "D" + " " + ([INSTALLATIONDATE][-2:]) +  "  "  +  [WORKORDERNUMBER]

The 5 fields I'm using here all have a value.  I want to add the inserted information (INSERTEDMAINSIZE and INSERTEDMAINMATERIAL)  if applicable, on another line ("r\n").  Where would I put the join(filter(None.... to get my final label?

0 Kudos
DarrenWiens2
MVP Honored Contributor

Basically, ' '.join(x) makes a new string inserting a space between the items in x.

filter(None,y) removes None from a tuple, y. You need to make a new tuple to fit your needs.

return ' '.join(filter(None, THE_TUPLE_TO_REMOVE_NONE_FROM))

 

MikeHutchison
New Contributor

Ok I got it to work, last question, where can I enter in [-2:] so I get the last 2 digits of the date?  In red I've included what I've tried and it failed.  I haven't had any formal python training so I'm trying to teach myself.  I appreciate your help.

def FindLabel ( [NOMINALDIAMETER], [MAINMATERIAL], [ABANDONMENTDATE], [ABANDONMENTORDER] :(
  return [NOMINALDIAMETER] + " " + [MAINMATERIAL] + " " + ' ' .join(filter(None, (([ABANDONMENTDATE][-2:]), [ABANDONMENTORDER])))

0 Kudos
DarrenWiens2
MVP Honored Contributor

Is [ABANDONMENTDATE] a string or date type? The slice won't work with date, but should work with string. You may need to remove the parentheses immediately around [ABANDONMENTDATE][-2:] .

0 Kudos
MikeHutchison
New Contributor

It's a date field

0 Kudos
DarrenWiens2
MVP Honored Contributor

Actually, [ABANDONMENTDATE][-2:] should work as a date or string. Just remove the parentheses immediately surrounding it.

MikeHutchison
New Contributor

This is what I've got and I'm getting an error.  I've tried both expressions and both got the same error message.

def FindLabel ( [NOMINALDIAMETER], [MAINMATERIAL], [ABANDONMENTDATE], [ABANDONMENTORDER] :(
  return [NOMINALDIAMETER] + " " + [MAINMATERIAL] + " " + ' '.join(filter(None, [ABANDONMENTDATE][-2:], [ABANDONMENTORDER]))

def FindLabel ( [NOMINALDIAMETER], [MAINMATERIAL], [ABANDONMENTDATE], [ABANDONMENTORDER] :(
  return ' '.join(filter(None, ([NOMINALDIAMETER], [MAINMATERIAL], ([ABANDONMENTDATE][-2:]), [ABANDONMENTORDER])))

0 Kudos
DarrenWiens2
MVP Honored Contributor

I think you're just having issues with grouping. This is what you had in the first example, which is passing 3 parameters to filter (filter(None,Field1,Field2)), which only takes 2 parameters:

' '.join(filter(None, [ABANDONMENTDATE][-2:], [ABANDONMENTORDER]))

I think it should be like this (filter(None,(Field1,Field2)):(

' '.join(filter(None, ([ABANDONMENTDATE][-2:], [ABANDONMENTORDER])))

0 Kudos