2

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)
Midavalo
30k11 gold badges53 silver badges108 bronze badges
asked Oct 3, 2016 at 16:52
1
  • You might try an "else:" at the end with DAMAGING_PARTY == DAMAGING PARTY ? I wouldn't expect that to work though. If not, you are going to have to either loop through with a cursor, or do a selection on each of the possible values (I've done the latter). Commented Oct 3, 2016 at 17:05

2 Answers 2

6

You are setting output = None at the beginning of your script, so anything that doesn't specifically set output is returned as a NULL 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 an else to return the current value.

codeblock = """def replacestring(DAMAGING_PARTY):
 if DAMAGING_PARTY == "Arizona Pipeline contact is Chad":
 return "Arizona Pipeline Company"
 elif DAMAGING_PARTY == "James A Shirley Construction":
 return "James A. Shirley Construction, Inc."
 elif DAMAGING_PARTY == "NPL Construction (Kevin Sena)":
 return "NPL Construction"
 elif DAMAGING_PARTY == "NPL Construction DENNIS PARKHOUSE":
 return "NPL Construction"
 elif DAMAGING_PARTY == "Rami / Benito Sustaita":
 return "Rami Alabassi"
 elif DAMAGING_PARTY == "Shea Homes (Contractor Working for)":
 return "Shea Homes"
 elif DAMAGING_PARTY == "Shea Homes/Rudy Andrade":
 return "Shea Homes"
 elif DAMAGING_PARTY == "W A RASIC CONSTRUCTION/CODY ROLLER":
 return "W A RASIC CONSTRUCTION"
 elif DAMAGING_PARTY == "WARASICK CONSTR COMPANY/GIANCARLO PAMDOSI":
 return "W A RASIC CONSTRUCTION"
 elif DAMAGING_PARTY == "Young Construction; contact Rick Musmecci":
 return "Young Construction"
 else:
 return DAMAGING_PARTY 
 """
answered Oct 3, 2016 at 17:02
4

You start by setting output = None inside of your function. You then change the value of output to some other value based on your if/else statements. For every case that is not specifically called out in your function you are still left with output = None.

So, when it comes time to return output you end up returning and writing NULL.

answered Oct 3, 2016 at 17:02
2
  • Are you suggesting that I remove output = None and return output? Commented Oct 3, 2016 at 17:03
  • 1
    @Midavalo already has a solid answer with updated code, but I just wanted to mention that you always need to return something, even if that something is 'NULL'. In this case you just want to return the value that already exists in the field. Commented Oct 3, 2016 at 20:04

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.