Hello!
I've just started learning Python and am having a lot of trouble figuring out how to write this code/what I'm doing wrong.
This is a if/elif and I want to calculate the same field for each feature class. For the last feature class (PLANNING_WAY), I want to add 2 fields together (QSTRM and LLD). The problem I keep encountering is, the field LLD has null values, and once my script hits that row, it stops and says "process finished with exit code 1".
Therefore, I've tried rewriting my code so that if the LLD value is None, it will just populate with the QSTRM field. Now, it doesn't populate the field for PLANNING_WAY at all.
import arcpy
arcpy.env.workspace = input("Workspace GDB: ")
# List feature classes and fields for gdb and create variables
fcList = arcpy.ListFeatureClasses(input("List of vector dataset names to process (* for all): "))
print("Processing {}".format(fcList))
fieldnm = "EXCL_NM"
exp_type = "PYTHON3"
# Update EXCL_NM field based on fc name using Field Calculator to concatenate field(s)
print("Populating EXCL_NM field")
for fc in fcList:
if fc == "TEST_land":
arcpy.CalculateField_management(fc, fieldnm, '!P_LABEL!', exp_type)
elif fc == "ADMIN_surface":
arcpy.CalculateField_management(fc, fieldnm, '!Name1!', exp_type)
elif fc == "PLANNING_WAY":
expression = "calc(!QSTRM!, !LLD!)"
codeblock = """
def calc(QSTRM, LLD)
if LLD is None:
return QSTRM
else:
return QSTRM + " " + LLD
"""
arcpy.CalculateField_management(fc, fieldnm, expression, exp_type, codeblock)
What am I doing wrong with this code? I am running this in PyCharm.
Thank you!