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 ?
Solved! Go to Solution.
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:
Now labels appear:
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 ....
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 ?
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!
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'>
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!
See a freewheeling discussion of these issues -- Much Ado About Nothing
Ahhh yes... brings back fond memories when we had Nothing better to do than philosophize
GeoNet at its best - I learned a lot from that thread.
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]