Field Calculate syntax error

969
10
01-14-2022 11:04 AM
2Quiker
Occasional Contributor II

Trying to figure out a simple field calculate but I can't i keep getting syntax error. This is a shapefile.

 

I need to update the updateField field. The updateField is a text field.

 

def Reclass(SC):
    if (SC == "GR"):
        return = GRAIN
    if (SC == "WR"):
        return = WRITE
    if (SC == "PL"):
        return = PLACE

updateField = Reclass(!SC!)

 

0 Kudos
10 Replies
AdrianWelsh
MVP Honored Contributor

If you're trying to return text, do you need quotes around your text returns?

0 Kudos
2Quiker
Occasional Contributor II

I have tried it with quotes and still get the syntax error.

def Reclass(SC):
    if (SC == "GR"):
        return "GRAIN"
    if (SC == "WR"):
        return "WRITE"
    if (SC == "PL"):
        return "PLACE"

updateField = Reclass(!SC!)
0 Kudos
by Anonymous User
Not applicable

Hello 2Quiker,

Does the syntax error identify a certain line of code?

I wonder if your return statements need to also be in quotations to be returned as text. "GRAIN" instead of GRAIN.

Cheers,

Mike

0 Kudos
2Quiker
Occasional Contributor II

Total rookie mistake, I forgot to check the python parser box.

AdrianWelsh
MVP Honored Contributor

Oh no! Was it in vbscript or Arcade? At least you got it figured out!

0 Kudos
2Quiker
Occasional Contributor II

It was in vbscript, so I though I had it figured out but I guess not. The following only works with one if but when adding more elif I get an error that field is not nullable. How do I handle Nulls and blanks in field calculator? The SC field dose have some nulls and blanks.

 

def Reclass(SC):
    if (SC == "GR"):
        return "GRAIN"
    elif (SC == "WR"):
        return "WRITE"
    elif (SC == "PL"):
        return "PLACE"

updateField = Reclass(!SC!)

 

 

0 Kudos
AdrianWelsh
MVP Honored Contributor

What do you want to do with nulls? Leave them null? The python syntax for null is "None".

So you could write something like:

if SC is not None:

    If (SC == "GR"):

        return "GRAIN"

    elif .... and so on

0 Kudos
2Quiker
Occasional Contributor II

Using what you posted if SC is not None: but still have me the error "The field is not nullable". So I check the attributes filed and there are some blanks and nulls. I am trying to run this on the how feature class table not just the selected features.

def Reclass(SC):
 if SC not in [None, "", " "]:
    if (SC == "GR"):
        return "GRAIN"
    elif (SC == "WR"):
        return "WRITE"
    elif (SC == "PL"):
        return "PLACE"

updateField = Reclass(!SC!)

 

0 Kudos
DanPatterson
MVP Esteemed Contributor
def Reclass(SC):
    if SC not in [None, "", " "]:
        if (SC == "GR"):
            return "GRAIN"
        elif (SC == "WR"):
            return "WRITE"
        elif (SC == "PL"):
            return "PLACE"
    else:
        return "grief a null"
  

you need to tell it what to do if there is one of those no-no cases


... sort of retired...
0 Kudos