Arcmap Python Label Expression Function "Null"

8749
30
Jump to solution
11-02-2016 03:26 PM
DevinUnderwood2
Occasional Contributor

I am having difficulty with python parser label function to replace null values with "Unknown Plan" and leave everything else as is. The Unknown Plan is not displaying in the map label.

I have the following:

def FindLabel ( [PLAN] ):
    if  ([PLAN]) is  None:
         return "Unknown Plan"
    else:
        return [PLAN]

Any idea it is not working ?

0 Kudos
30 Replies
ScottFraser3
New Contributor III

This was helpful and allowed me to find the solution.  For the sake of clarity, for those to come in the future...

Problem: The label expression included fields for "Building numbers" and "Unit numbers."  However, labels only appear where there were actual data entries in those fields. Regular addresses [single family home at 123 Main St.] had no labels displayed because there wasn't date entered for either a "building number" nor a "unit number".  Those empty fields were "<Null>".

Solution:

  • Select all records where "building number" and/or "unit number" were "<Null>. 
  • Calculate these fields to fill with blank strings [" "]
  • Voilà!

Now labels appear:

  • For all locations, regardless if there are building or unit numbers in those related fields.
  • Locations with building and/or unit info, also have this data appearing in the labels.
0 Kudos
TedKowal
Occasional Contributor III

What happens when there are multiple spaces......

Would not [PLAN].strip() in (None, "") work better and a little more bullet proof?

------ I still say Python has issues with nulls, empty, NA  and spaces ....

DevinUnderwood2
Occasional Contributor

I agree that in general, nulls, empty, NA  and spaces always present issues which I don't remember how to address right away.

Technically, my empty fields do not have spaces since I never typed anything in, I would assume?

So, you bring up a good point, what exactly are the empty fields represented by ?

0 Kudos
TedKowal
Occasional Contributor III

For me the empty cells would be null, lot's depends on how the database/tables have been defined...

Using the old VBA scripting language this was never an issue...but with python (I have to check for everything!)   Not to mention unprintable control characters.... but this is off subject......   I am a very reluctant python users.... vb rest in peace!

0 Kudos
DanPatterson_Retired
MVP Emeritus

As you said ... 'old vba scripting' .... 

In python there is only one None, everything is an empty version of a type...

a refresher... /blogs/dan_patterson/2016/11/03/before-i-forget-18-those-pesky-null-things 

and a clear statement

>>> type("")
<class 'str'>
>>> type(None)
<class 'NoneType'>‍‍‍‍
0 Kudos
TedKowal
Occasional Contributor III

But in practice it does not seems to work out that way!  I have a multi page function that I am building that test all sorts of nasties.... that should have been picked up by "None".     -- lots of your refresher stuff found its way in my function!  (I mean def)

Miss my onesize fits all code from vb ...  if  trim([myfield]) & "" = "" then ...  Never really had to test for blank or null stuff.....ahhh but life moves on!

0 Kudos
curtvprice
MVP Esteemed Contributor

See a freewheeling discussion of these issues --  Much Ado About Nothing

DanPatterson_Retired
MVP Emeritus

Ahhh yes... brings back fond memories when we had Nothing better to do than philosophize

curtvprice
MVP Esteemed Contributor

GeoNet at its best - I learned a lot from that thread.

DevinUnderwood2
Occasional Contributor

Great feedback, thanks.

With your help I was able to use the following python label expression in my mapbook.

For any water pipeline attributed with, "abandon", do not label.  Also, if any pipeline is null then label as seen below.

def FindLabel ([PLAN], [AGENCY]):
    if [AGENCY] == "ABANDON":
        return None
    elif [PLAN] in  (None, " "):
        return "Plan ?"
    else:
        return [PLAN]‍‍‍‍‍‍‍

I  need to also only show pipeline labels for pipe greater than 50 feet. I tested out the following and it doesn't work. I would then need to add it to my current expression.

Any ideas what is wrong with my expression.

Thanks

def FindLabel ( [SHAPE.LEN], [PLAN]  ):
    if [SHAPE.LEN]  > 50:
        return [PLAN]
    else:
        return [PLAN] ‍‍‍‍‍
0 Kudos