if/else statements in field calculator

609
6
Jump to solution
01-26-2021 01:26 PM
KathleenHoenke
Occasional Contributor

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")

  

0 Kudos
3 Solutions

Accepted Solutions
JoeBorgione
MVP Esteemed Contributor

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.

That should just about do it....

View solution in original post

DanPatterson
MVP Esteemed Contributor
if fc[:2] == "PG":

slicing alternative... compare the first 2 characters and == is the equality check


... sort of retired...

View solution in original post

DanPatterson
MVP Esteemed Contributor

your code block has to be enclosed in triple quotes """def..... to the end """

Calculate Field (Data Management)—ArcGIS Pro | Documentation

see the examples


... sort of retired...

View solution in original post

6 Replies
DavidPike
MVP Frequent Contributor

I think you need a regex for wildcard matches:
How to implement wildcards in Python (educative.io)

JoeBorgione
MVP Esteemed Contributor

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.

That should just about do it....
DanPatterson
MVP Esteemed Contributor
if fc[:2] == "PG":

slicing alternative... compare the first 2 characters and == is the equality check


... sort of retired...
KathleenHoenke
Occasional Contributor

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

  

0 Kudos
DanPatterson
MVP Esteemed Contributor

your code block has to be enclosed in triple quotes """def..... to the end """

Calculate Field (Data Management)—ArcGIS Pro | Documentation

see the examples


... sort of retired...
KathleenHoenke
Occasional Contributor

Thank you!

0 Kudos