Hi there,
I'm a newbie for python, trying to fill the <Null> into 04 by using the "Caculate Field" function.
But it says I got some invaild syntax, not sure what's goion on after searching some tutorials on the internet.
I wan't to know how to make it work, thanks.
Sincerely,
Teng-Wei.
Solved! Go to Solution.
re .... is the name of a python module change the function name, or perhaps use the following
re .... is the name of a python module change the function name, or perhaps use the following
Dan's method above of selecting target records and then calculating on the selected records is a safe, easy and effective way to do this. Be sure to work on a copy of data when experimenting or alternately make sure that Undo is enabled so you can Discard edits when things go wrong.
Consider the option below:
Python is fussy about indentation and colons:
!LOC! if !LOC! else "04"
Will do the same since <NULL> is effectively None
emulating a file in a table called LOC
LOC =['A', 'B', None, "D"]
[i if i else "04" for i in LOC]
Out[12]: ['A', 'B', '04', 'D']
As pointed out by @BarryNorthey the specific syntax error you are getting is due to a lack of a colon after your function definition line.
Since you are new to Python, I will also point out that best practice according to https://peps.python.org/pep-0008/ :
Comparisons to singletons like None should always be done with is or is not, never the equality operators.
So, in this case your syntax should not be
if ( LC01 == None ) : # which by the way you forgot another colon.
but should be
if ( LC01 is None ) :
As @DanPatterson has already pointed out, for a simple conditional check like this you can rely on Python's conditional expression, aka ternary operator, to handle it rather than defining a function in the code block.
"04" if !LC01! is None else !LC01!