1

I've written a simple python function that outputs a list based on yes/no fields. It appends a word to a list if it has a "yes" in another column then writes the list, formatted according to the number of values in it.

It looks like this:

def proctype(prestm, presdm, postm, posdm, beam, wem):


    mylist = []
    if prestm == 'YES':
        mylist.append("PreSTM")
    if presdm == 'YES':
        mylist.append("PreSDM")
    if postm == 'YES':
        mylist.append("PoSTM")
    if posdm == 'YES':
        mylist.append("PoSDM")
    if beam == 'YES':
        mylist.append("Beam")
    if wem == 'YES':
        mylist.append("WEM")


    if len(mylist) == 1:
        return mylist[0]
    if len(mylist) == 2:
        return mylist [0] + " and " + mylist[1]     
    if len(mylist) == 3:
        return mylist[0] + ", " + mylist[1] + " and " + mylist[2]
    if len(mylist) == 4:
        return mylist[0] + ", " + mylist[1] + ", " + mylist[2] + " and " + mylist[3]
    if len(mylist) == 5:
        return mylist[0] + ", " + mylist[1] + ", " + mylist[2] + ", " + mylist[3] + " and " + mylist[4]
    if len(mylist) == 6:
        return mylist[0] + ", " + mylist[1] + ", " + mylist[2] + ", " + mylist[3] + ", " + mylist[4] + " and " + mylist[5]

This works exactly as I want it to if the fields its looking at are 'yes' and 'no', but if there are any Null values in the six input fields the output will always be null.

Does anyone know why this is? Can the field calculator not handle Null inputs, or does the list.append have a problem with Null values?

I've tried setting the values to a number if theyre null like this:

if prestm is None:
    prestm = 1

But that doesn't help.

EDIT: This problem only occurs when the inputs are from other fields in the table. inputing "" works but

proctype( !PRESTM!, !PRESDM!, !POSTM! , !POSDM! , !BEAM_MIGRATION! , !WEM_RTM! )

doesnt.

EDIT 2: The problem is not with my code it is with passing in other attributes as parameters where any of them are Null

def testing(prestm, presdm, postm, posdm, beam, wem):
    return "test ok"

Returns "test ok" when all of the fields are populated but null

if any of the input fields are Null

  • 1
    Your code worked for me... I tried setting prestm = None and prestm = "" the function returned as expected. – Hornbydd Sep 22 '14 at 14:36
  • I've just tried the same - typing the parameters manually and it works. The problem therefore must be with the field calculator reading columns which are Null. proctype( !PRESTM!, !PRESDM!, !POSTM! , !POSDM! , !BEAM_MIGRATION! , !WEM_RTM! ) is what doesn't work – whatahitson Sep 22 '14 at 14:46
  • 1
    May or may not end up being relevant, but, it may be useful to know what your data source is (are you trying to do this with a File GDB, Personal GDB, SQL Server 2008R2 SDE, etc...)? – John Sep 22 '14 at 14:57
  • 1
    Whether this solves the problem or not, remove the space between mylist and [0] for the statement that reads: return mylist [0] + " and " + mylist[1] – Richard Fairhurst Sep 22 '14 at 15:39
  • 1
    Add the clause len(mylist) == 0: and return some value to tell you all values were not 'Yes'. If the return value is Null the calculation must have an error. If it is the value you specified for the empty list you know the list was never populated. – Richard Fairhurst Sep 22 '14 at 15:49

Your Answer

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

Browse other questions tagged or ask your own question.