Error running field calculator

1114
4
07-12-2011 10:31 AM
deleted-user-rQoEFM5qzbHE
New Contributor II
I am attempting to copy over values from a string field to an integer field. In the case of an empty string, I want to make the value null but am getting the following error:

ERROR 000539: Error running expression: getDOT( " ") <type 'exceptions.NameError'>: global name 'null' is not defined
Failed to execute (Calculate Field (7))

The code block that I am using the field calculator is:

def getDOT(dot):

  if (len(dot) > 1):
    result = int(dot)
  else:
    result = null

  return result


Is it possible to set integer field to a null value? I have done it in the past, but cannot seem to get it to work now.

Thanks,

jeremy
Tags (2)
0 Kudos
4 Replies
TerrySilveus
Occasional Contributor III
still learning python myself, but you might try None instead of null
0 Kudos
deleted-user-rQoEFM5qzbHE
New Contributor II
Here is another issue I am having. This string field has values that are not numbers. For example, one of the records has a value of 'UNKNOWN'. Since I am copying these values over to an integer field, this record should have a value of 0. However, I cannot figure out how to catch for this type of situation. Here is the code I am trying to use:

def getDOT(dot):

  if (isinstance(int(dot), int)):
    result = int(dot)
  else:
    result = None

  return result


It doesn't like the second line.When it tries to convert the above example to an int, it crashes. This makes sense, but I cannot seem to figure out another way to approach this problem. Anybody have any ideas?

Thanks,

jeremy
0 Kudos
ChristopherFricke1
New Contributor
You should use a try/except statement http://docs.python.org/tutorial/errors.html
def getDOT(dot):

def getDOT(dot):
    try:
        result = int(dot)
    except:
        result = Null
    finally:
        return result
0 Kudos
TerrySilveus
Occasional Contributor III
and I'm not sure of your purposes, but you may not want to make an "UNKNOWN" 0 because 0 may be a legitimate value, it would probably be better to make that value NULL which christophfricke's code should do for you.
0 Kudos