0

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

Results using the expression in Python IDLE

asked Jun 18, 2020 at 16:31

2 Answers 2

4

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 an else)
  • 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.

answered Jun 18, 2020 at 21:59
0

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]
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jun 18, 2020 at 17:10
3
  • 2
    !Street! and !Street_ID! should be passed into function f as parameters (f(!Street!,!StreetID!,'defVal'), and function f should be defined with three parameters def 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]) Commented Jun 18, 2020 at 18:43
  • Thanks for catching that. Commented Jun 18, 2020 at 18:54
  • 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. Commented Jun 18, 2020 at 20:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.