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.

enter image description here

enter image description here

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)
  • Try if s <> '' and s is not None: – Erica Sep 5 '14 at 12:28
  • Thanks I just tried your suggestion, i also tried the following but none of them worked if (s <> '' and s is not None): if s <> '' and s is not "None": – user21053 Sep 5 '14 at 13:24
  • How about conditional if t is not None: before it converts t to a string s = str(t).strip() --- or if s <>'' and s <> 'None': – Erica Sep 5 '14 at 13:34
  • yep that worked now with the 'None' - it needed the quotations, as it is text - THANKS. – user21053 Sep 5 '14 at 14:57

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.

  • just tested this and it works as well -many thanks! – user21053 Sep 5 '14 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.