caculate field python invaild syntax

716
4
Jump to solution
01-28-2023 04:42 PM
WilliamYu811
New Contributor II

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.

螢幕擷取畫面 2023-01-29 082443.png

Sincerely,

Teng-Wei.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

re  .... is the name of a python module change the function name, or perhaps use the following

 

isnull.png

isnull2.png

  


... sort of retired...

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

re  .... is the name of a python module change the function name, or perhaps use the following

 

isnull.png

isnull2.png

  


... sort of retired...
0 Kudos
BarryNorthey
Occasional Contributor III

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:

BarryNorthey_0-1674960389660.png

 

DanPatterson
MVP Esteemed Contributor

!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']

... sort of retired...
JoshuaBixby
MVP Esteemed Contributor

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!