I am trying to use Python in ArcGIS Pro to cleanse some data via imported Excel sheets. The expression works, but the Code Block if-else statement to return nothing if !Street_Id! is returns an error.
Address
str(!Street_ID!) + ' ' + !Street!.split("/")[0]
Code Block
def f(x): ' '
if !Street_ID! = 'Null'
return x
else return str(!Street_ID!) + ' ' + !Street!.split("/")[0]
Error
File"<string>",line 2
if!Street_ID! = 'Null'
^
SynatxError: invalid syntax
2 Answers 2
There's a bunch of things wrong with your code:
- Incorrect code invocation
- Incorrect function definition (placing a non-comment to the right of the colon)
- Poor function naming (far better to keep everything you use, so you don't have to debug every time you use it, but that requires descriptive naming)
- Failure to pass in the values to be used in the expression string
- No indentation (required part of the Python language)
- Failure to place a space between the
if
keyword and logical expression - Using assignment (
=
) instead of equivalence (==
) for a logical test - Using expression field delimiters in Python code
- Assuming a NULL value will match the string 'Null' (instead it maps to
None
) - Improper use of early exit (after a
return
you don't need anelse
) - Performing string math (using
format
is best practice)
When the smoke clears, the corrected result looks like this:
Expression:
usefulName(!Street!,!StreetID!,'nullValue')
Expression type:
Python
Code block:
def usefulName(street_val,street_id,x):
if (street_id == None):
return x
return "{:s} {:s}".format(street_id,street_val.split("/")[0])
If you want to avoid early exit (some organizations/programs forbid it as confusing), then the code block would look like:
def usefulName(street_val,street_id,x):
result = x
if (street_id != None):
result = "{:s} {:s}".format(street_id,street_val.split("/")[0])
return result
And the ternary logic form would look like:
def usefulName(street_val,street_id,x):
return "{:s} {:s}".format(street_id,street_val.split("/")[0]) if street_id else x
Which actually compresses down to a simple Python expression of:
"{:s} {:s}".format(!StreetID!,!Street!.split("/")[0]) if !StreetID! else 'nullValue'
though that's probably a bit obscure for reuse.
You need a double equal sign, a colon at the ends, and proper indentation for sure.
Consider writing your code in a Python IDE that shows syntax errors during code development.
def f(x):
if !Street_ID! == 'Null':
return x
else:
return str(!Street_ID!) + ' ' + !Street!.split("/")[0]
-
2
!Street!
and!Street_ID!
should be passed into functionf
as parameters (f(!Street!,!StreetID!,'defVal')
, and functionf
should be defined with three parametersdef f(street_val,street_id,x)
and therefore won't have the bang sandwich inside the function (return str(street_id) + ' ' + street_val.split("/")[0]
)Vince– Vince2020年06月18日 18:43:21 +00:00Commented Jun 18, 2020 at 18:43 -
-
I am going to research to comprehend the way Vince explained passing into functions as parameters and defining the f function with parameters and will post my answer after the fact, I get what he is saying generally when using parameters, but at the same time I need to comprehend how to script it out and why it needs to be expressed in the code block concerning indentation. I did catch onto the bang sandwich at least.cheff_– cheff_2020年06月18日 20:17:03 +00:00Commented Jun 18, 2020 at 20:17
Explore related questions
See similar questions with these tags.