I am trying to repeat the behavior of this code from the ESRI website.
I want to replicate the output example in ALL_Type
field, so that it concatenates a field but it deals (ignores) any that have NULL values.
Unfortunately this example no longer works at 10.2.2 because all example of NULL return a "None" text. Instead I just want a blank to show.
And here is the code.
"*args" allows this routine to accept any number of field values.
# the values are passed as a Python tuple, essentially a
# non-editable list
#
def concat(*args):
# Initialize the return value to an empty string,
# then set the separator character
#
retval = ""
sep = "_"
# For each value passed in...
#
for t in args:
# Convert to a string (this catches any numbers),
# then remove leading and trailing blanks
#
s = str(t).strip()
# Add the field value to the return value, using the separator
# defined above
#
if s <> '':
retval += sep + s
# Strip of any leading separators before returning the value
#
return retval.lstrip(sep)
Answer
I think this should do what you want:
def concat(*args):
sep = "_"
nonnull_args = [str(arg).strip() for arg in args if arg] # Filter NULLs
good_args = [arg for arg in nonnull_args if arg] # Filter blanks
retval = sep.join(good_args)
return retval
The args just get passed through a couple filters and joined by sep
at the end.
No comments:
Post a Comment