I'm trying to label point features with ArcGIS Maplex Engine, using [field1], [field2], [field3], [datefield]. My code is set up to label [field1] + '\n' + [datefield][-2:]. If [field1] is null, it'll label [field2] + '\n' + [field3] + '\n' + [datefield][-2:]. For whatever reason, my code doesn't work if a feature has a null value in [datefield]. How do I tell my code if the [datefield] is null, label [field1] AND if [datefield] and [field1] is null, label [field2] + '\n' + [field3]? This is my code so far.
def FindLabel ( [field1], [field2], [field3], [datefield] 😞
if str([field1]) == "None" :
return [field2] + '\n' + [field3] + '\n' + [datefield][-2:]
elif str([field1]) != "None" :
return [field1] + '\n' + [datefield][-2:]
I tried adding this section but the engine doesn't seem to read it.
elif [datefield] == "None" :
return [field1]
elif [datefield] == "None" and str([field1]) == "None" :
return [field2] + '\n' + [field3]
My entire code is this.
def FindLabel ( [field1], [field2], [field3], [datefield] 😞
if str([field1]) == "None" :
return [field2] + '\n' + [field3] + '\n' + [datefield][-2:]
elif str([field1]) != "None" :
return [field1] + '\n' + [datefield][-2:]
elif [datefield] == "None" :
return [field1]
elif [datefield] == "None" and str([field1]) == "None" :
return [field2] + '\n' + [field3]
Thanks for the help.
One of my last resort tests when nothing else seems to work is equating null to blank
myfield1 = [Field1]+""
if myfield1.strip() = "" then
'This could be null'
.....
"Python Nulls Strike back!"
I received an error with this:
field1 = None
myfield = field1+""
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
Ted is maybe using so VB voodoo or the file doesn't contain None's but empty strings. so you would have to do
str(field1) + "" which is pretty useless when you can check for NoneType and/or empty anything with the added conversion.
And other issues possible with str(None) which equals "None":
field1 = None
myfield = str(field1)+""
print len(myfield), myfield
4 None
I never endorsed the str(None) at all, Randy, just too many things that can be exceptions. Basically with tabular data, work with either checks for the empty string or None using isinstance. This covers shapefiles, file gdb's and numpy, scipy, matplotlib, etc etc at least... everything else is (server stuff etc) is on its own for me
... sorry did not convert to Python'ese '-'.join(filter(bool,(field1,"")))
--- and no it is not recommended only if the trustworthy None does not work (field contains unprintable artifact values).
Back to Michael's question. Check for type None and an empty (zero-length) string.
field1 = "" # "Field1"
field2 = "Field2"
field3 = "" # "Field3"
datefield = "2014-12-13"
if (field1 is not None):
# check for empty string (assuming field1 is a string type)
if not len(field1):
field1 = None
print "field1 set to none"
# perhaps a similar check for datefield, etc.
if ( (field1 is not None) and (datefield is not None) ):
print field1 + '\n' + datefield[-2:]
elif (datefield is not None):
print '\n'.join(filter(None,(field2, field3, datefield[-2:])))
else:
print '\n'.join(filter(None,(field2, field3)))
I don't recall him worried about empty strings, just null attribute table values causing issues in the label. Anyone have fault with my method I posted above?
Sorry, I may have gotten off topic. It was just to clarify the difference between null, type None, and an empty string.
Like you I thought this turned into a case where None simply does not work as expected ... I do run across that quite often when I deal with data from multiple sources and platforms ....
... It is an important distinction knowing Null from Empty String.
max(None,"") #Returns blank
max("Hello","") #Returns Hello
Helps me quickly determine Nulls and Empty stings ....