ArcGIS Pro 2.9 Advanced label expression / code block question

747
5
01-21-2022 01:45 PM
KeithAddison1
Occasional Contributor III

Anyone know what on earth is wrong with this label expression code block?  Once I get the error fixed, how on earth do I invoke it?  ArcMAP had the pre logic script code bit separate from the expression, but now its not, how does it work now in Pro? (the help files seem to totally gloss it over)

KeithAddison1_1-1642801533300.png

 

0 Kudos
5 Replies
KarstenRank
Occasional Contributor III

Hi,

if needs == for equal in python.

Greetings Karsten

by Anonymous User
Not applicable

Your example doesn't look like valid Python?

Something like:

def FindLabel ( [UNIX Time] ):
    if [UNIX Time] in ["TOM THUMB", "WILLIAM TELL"]:
        return [UNIX Time]
    else:
        return [UNIX Time]

 

jcarlson
MVP Esteemed Contributor

A single "=" in Python is very different from "==", which is what you want to do to check the value of a variable. You also need to give both conditions in full. Anything after the "OR" needs to evaluate to a boolean on its own.

 

def FindLabel ([NAME1]):
    if [NAME1] == "TOM THUMB" OR [NAME1] == "WILLIAM TELL":
        return [NAME1]
    else:
        return " "

 

Alternatively, you can check if the field is in a list of "match" values.

 

def FindLabel ([NAME1]):
    if [NAME1] in ["TOM THUMB", "WILLIAM TELL"]:
        return [NAME1]
    else:
        return " "

 

- Josh Carlson
Kendall County GIS
Luke_Pinner
MVP Regular Contributor

Please post code (and code block expressions) as formatted code not screenshots, makes it so much easier for people to read, copy, test and then hopefully answer.

https://community.esri.com/t5/python-blog/code-formatting-the-community-version/bc-p/1135971

0 Kudos
DanPatterson
MVP Esteemed Contributor
!your_fld! if !your_fld! in ["Tom Thumb", "William Tell"] else " "

maybe?


... sort of retired...