I am using field calculator to change several values of field. I am receiving NULL values for those which are not in my function. What is the cause of this and a workaround?
In the code below; I would like to change only the values in my function. If a value is not in the function; the code automatically sets the new value as NULL.
import arcpy
codeblock = """def replacestring(DAMAGING_PARTY):
output = None
if DAMAGING_PARTY == "Arizona Pipeline contact is Chad":
output = "Arizona Pipeline Company"
elif DAMAGING_PARTY == "James A Shirley Construction":
output = "James A. Shirley Construction, Inc."
elif DAMAGING_PARTY == "NPL Construction (Kevin Sena)":
output = "NPL Construction"
elif DAMAGING_PARTY == "NPL Construction DENNIS PARKHOUSE":
output = "NPL Construction"
elif DAMAGING_PARTY == "Rami / Benito Sustaita":
output = "Rami Alabassi"
elif DAMAGING_PARTY == "Shea Homes (Contractor Working for)":
output = "Shea Homes"
elif DAMAGING_PARTY == "Shea Homes/Rudy Andrade":
output = "Shea Homes"
elif DAMAGING_PARTY == "W A RASIC CONSTRUCTION/CODY ROLLER":
output = "W A RASIC CONSTRUCTION"
elif DAMAGING_PARTY == "WARASICK CONSTR COMPANY/GIANCARLO PAMDOSI":
output = "W A RASIC CONSTRUCTION"
elif DAMAGING_PARTY == "Young Construction; contact Rick Musmecci":
output = "Young Construction"
return output"""
ShortCodeExpression = "replacestring(!DAMAGING_PARTY!)"
table = r'table'
arcpy.CalculateField_management(table, "DAMAGING_PARTY", ShortCodeExpression, "PYTHON_9.3", codeblock)
Best Answer
You are setting
output = None
at the beginning of your script, so anything that doesn't specifically setoutput
is returned as aNULL
at the end when you return the value. Instead of setting output at the start, just return each value in your if/elif chain, and then anelse
to return the current value.