Beginner Python Code Questions

162
5
Jump to solution
07-07-2022 03:27 PM
CarolynGao15
New Contributor II

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! 

0 Kudos
2 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
return QSTRM + " " + LLD

needs to be indented 4 spaces.

also

 

a, b = None, None  # two fields one or both with None in them
# concatenate with _

f'{a}_{b}'
'None_None'

# hence, no need to check for None when converting to string format

... sort of retired...

View solution in original post

DavidSolari
Occasional Contributor

One thing you can try is checking if the code block is causing issues. You can replace the block with this 1-line expression:

"{} {}".format(!QSTRM!, !LLD!) if !LLD! is not None else !QSTRM!

 

View solution in original post

5 Replies
DanPatterson
MVP Esteemed Contributor
return QSTRM + " " + LLD

needs to be indented 4 spaces.

also

 

a, b = None, None  # two fields one or both with None in them
# concatenate with _

f'{a}_{b}'
'None_None'

# hence, no need to check for None when converting to string format

... sort of retired...
CarolynGao15
New Contributor II

Thank you! I completely missed that. 

If you don't mind, could you explain the second part? 

Would I plug that in to the expression part of my arcpy.management.CalculateField line?

0 Kudos
DanPatterson
MVP Esteemed Contributor
# given two variables, a and b, or fields if denoted by !a! and !b!
# if either of them are None, convert them to their string representation
# using python's mini format syntax, which converts any object to string

f'{a}_{b}'
'None_None'

# you can do condition checking and provide alternate formatting.
# note.... remove the _ if you don't want it to appear in the output

a, b = None, None
f"{'' if a is None else a}_{'' if b is None else b}"
'_'

a, b = None, "Something"
f"{'' if a is None else a}_{'' if b is None else b}"
'_Something'

... sort of retired...
DavidSolari
Occasional Contributor

One thing you can try is checking if the code block is causing issues. You can replace the block with this 1-line expression:

"{} {}".format(!QSTRM!, !LLD!) if !LLD! is not None else !QSTRM!

 

CarolynGao15
New Contributor II

Thank you so much! This worked perfectly and cut down a whole block of code lol! 

0 Kudos