Calculate Values problems

701
4
Jump to solution
01-26-2014 01:44 PM
TimothyElliott
New Contributor III
I am trying to write a script to calculate values if there are null values in a table however it never calculates any values and I am pretty sure my code is right.
Where (Operator) is a field with values and null values
and (PointID) is a field with coded values and the Operator name as a 3 letter code stored within the text.

  Expression: Calc (!Operator!)  Code Block: def Calc (Operator):   while (Operator is null):     if (re.search ("DWS", PointID) is TRUE):       return "Don"     elif (re.search ("GMS", PointID) is TRUE):       return "Gordon"     elif (re.search ("JZL", PointID) is TRUE):       return "Julian"     elif (re.search ("AWM", PointID) is TRUE):       return "Anthony"     else return (Operator)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
TimothyElliott
New Contributor III
def Calc (Operator, PointID):   if (Operator == None):     if (re.search ("DWS", PointID) is TRUE):       return "Don"     elif (re.search ("GMS", PointID) is TRUE):       return "Gordon"     elif (re.search ("JZL", PointID) is TRUE):       return "Julian"     elif (re.search ("AWM", PointID) is TRUE):       return "Anthony"     else:       return (Operator)   else:     return (Operator)


I finally got it all to work thanks for pointing me in the right direction all needed to import the re function as its not native in ArcPy and the true statement is not required.

import re def Calc (Operator,PointID):   if (Operator == None):     if (re.search ("AWM",(PointID))):       return "Anthony"     elif (re.search ("AZS",(PointID))):       return "Andrea"     elif (re.search ("ERT",(PointID))):       return "Earl"     else:       return (PointID)   else:     return (Operator)

View solution in original post

0 Kudos
4 Replies
RichardFairhurst
MVP Honored Contributor
I am trying to write a script to calculate values if there are null values in a table however it never calculates any values and I am pretty sure my code is right.
Where (Operator) is a field with values and null values
and (PointID) is a field with coded values and the Operator name as a 3 letter code stored within the text.

 
Expression:
Calc (!Operator!)

Code Block:
def Calc (Operator):
  while (Operator is null):
    if (re.search ("DWS", PointID) is TRUE):
      return "Don"
    elif (re.search ("GMS", PointID) is TRUE):
      return "Gordon"
    elif (re.search ("JZL", PointID) is TRUE):
      return "Julian"
    elif (re.search ("AWM", PointID) is TRUE):
      return "Anthony"
    else return (Operator)


Your code is not right.  You did not feed the PointID into the code block through the expression, so the code block has no idea what PointID means.  Your code is wrong for detecting Null values within Python.  You don't need a while expression in the field calculator for the records it is calculating, since it iterates all records for the calculated field for you.  Finally, your have to return a value for every record calculated, not just Null records, which your code does not do.  To succeed the code needs to read:


Expression:
Calc (!Operator!, !PointID!)

Code Block:
def Calc (Operator, PointID):
  if (Operator == None):
    if (re.search ("DWS", PointID) is TRUE):
      return "Don"
    elif (re.search ("GMS", PointID) is TRUE):
      return "Gordon"
    elif (re.search ("JZL", PointID) is TRUE):
      return "Julian"
    elif (re.search ("AWM", PointID) is TRUE):
      return "Anthony"
    else return (Operator)
  else return (Operator)
0 Kudos
TimothyElliott
New Contributor III
Your code is not right.  You did not feed the PointID into the code block through the expression, so the code block has no idea what PointID means.  Your code is wrong for detecting Null values within Python.  You don't need a while expression in the field calculator for the records it is calculating, since it iterates all records for the calculated field for you.  Finally, your have to return a value for every record calculated, not just Null records, which your code does not do.  To succeed the code needs to read:


Expression:
Calc (!Operator!, !PointID!)

Code Block:
def Calc (Operator, PointID):
  if (Operator == None):
    if (re.search ("DWS", PointID) is TRUE):
      return "Don"
    elif (re.search ("GMS", PointID) is TRUE):
      return "Gordon"
    elif (re.search ("JZL", PointID) is TRUE):
      return "Julian"
    elif (re.search ("AWM", PointID) is TRUE):
      return "Anthony"
    else return (Operator)
  else return (Operator)


code says there is a syntax problem with line 11, the else statements.
0 Kudos
RichardFairhurst
MVP Honored Contributor
code says there is a syntax problem with line 11, the else statements.


def Calc (Operator, PointID):
  if (Operator == None):
    if (re.search ("DWS", PointID) is TRUE):
      return "Don"
    elif (re.search ("GMS", PointID) is TRUE):
      return "Gordon"
    elif (re.search ("JZL", PointID) is TRUE):
      return "Julian"
    elif (re.search ("AWM", PointID) is TRUE):
      return "Anthony"
    else:
      return (Operator)
  else:
    return (Operator)
0 Kudos
TimothyElliott
New Contributor III
def Calc (Operator, PointID):   if (Operator == None):     if (re.search ("DWS", PointID) is TRUE):       return "Don"     elif (re.search ("GMS", PointID) is TRUE):       return "Gordon"     elif (re.search ("JZL", PointID) is TRUE):       return "Julian"     elif (re.search ("AWM", PointID) is TRUE):       return "Anthony"     else:       return (Operator)   else:     return (Operator)


I finally got it all to work thanks for pointing me in the right direction all needed to import the re function as its not native in ArcPy and the true statement is not required.

import re def Calc (Operator,PointID):   if (Operator == None):     if (re.search ("AWM",(PointID))):       return "Anthony"     elif (re.search ("AZS",(PointID))):       return "Andrea"     elif (re.search ("ERT",(PointID))):       return "Earl"     else:       return (PointID)   else:     return (Operator)
0 Kudos