Is there a way to specify in an if/else statement "if fc begins with "PG", return 918? Does a * work for this? see below:
# Structure Class
Class = "StructureClass"
expression6 = "Reclass(!DAM_TYPE!)"
codeblock = def Reclass (fc):
if fc ="PG*":
return 918
elif fc = "CB*":
return 97
elif fc = "VA*":
return 98
elif fc = "MV*":
return 99
elif fc = "OT*":
return 917
else:
return None
arcpy.CalculateField_management(fc, Class, expression6, "PYTHON_9.3")
Solved! Go to Solution.
A couple things about if statements in python:
if something = value:
will never work.
if something == value:
is the way to do it.
I've never seen a wild card work in python, perhaps someone else can make it work for you. But this ought to get you going:
if fc.startswith('PG'):
I assume you have already defined the variable fc although you don't show it.
if fc[:2] == "PG":
slicing alternative... compare the first 2 characters and == is the equality check
your code block has to be enclosed in triple quotes """def..... to the end """
Calculate Field (Data Management)—ArcGIS Pro | Documentation
see the examples
I think you need a regex for wildcard matches:
How to implement wildcards in Python (educative.io)
A couple things about if statements in python:
if something = value:
will never work.
if something == value:
is the way to do it.
I've never seen a wild card work in python, perhaps someone else can make it work for you. But this ought to get you going:
if fc.startswith('PG'):
I assume you have already defined the variable fc although you don't show it.
if fc[:2] == "PG":
slicing alternative... compare the first 2 characters and == is the equality check
Thank you both! I feel as though I'm doing something foolish here and calling the wrong thing... - I greatly appreciate all of the help.
import arcpy
arcpy.env.workspace = r'F:\Analysis\InventoryWork\InventoryUpdateMay2020\New_Datasets_Schema.gdb'
#The NID plus SARP data path
fc = r'F:\Analysis\InventoryWork\InventoryUpdateMay2020\New_Datasets_Schema.gdb\NID2020_Schema'
Class = "StructureClass"
expression6 = "Reclass(!DAM_TYPE!)"
codeblock = def Reclass (fc):
if fc[:2]=="PG":
return 918
elif fc[:2]=="CB":
return 97
elif fc[:2] == "VA":
return 98
elif fc[:2] == "MV":
return 99
elif fc[:2] == "OT":
return 917
else:
return None
arcpy.CalculateField_management(fc, Class, expression6, "PYTHON_9.3")
File "<string>", line 4
codeblock = def Reclass (fc):
^
SyntaxError: invalid syntax
your code block has to be enclosed in triple quotes """def..... to the end """
Calculate Field (Data Management)—ArcGIS Pro | Documentation
see the examples
Thank you!