Select to view content in your preferred language

ArcGIS Pro 3.0.4 - Python Script Failing for Labels

1661
9
Jump to solution
03-15-2023 08:38 AM
AnthonyBomba
Emerging Contributor

I recently upgraded from 2.7 to 3.0.4. This code worked perfectly in 2.7 but seems to have stopped working after I upgraded. I've seen similar posts to this but I don't know if the issue was ever resolved. 

def FindLabel ( [SURFACTANTS], [AMMONIA], [CHLORINE] ):
surf, amm, chlor = "","",""
if float([SURFACTANTS]) >0.24:
surf = "Surfactants: " + [SURFACTANTS] + "\n"
if float([AMMONIA]) >= 0.5:
amm = "Ammonia: " + [AMMONIA] + "\n"
if float([CHLORINE]) >= 0.02:
chlor = "Chlorine: " + [CHLORINE]

return surf + amm + chlor

The error message I'm getting is:

Invalid expression.
Error on line 1.
Semicolon or new line expected

I'm not sure where a semicolon or new line is expected, but I'm hoping it's an easy fix

0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor

Well, it appears as if Pro labeling likes the [] instead of !! for the field names, However, the only  way I get the same error message is if I fail to switch the language to Python "before" I paste in the code.

def FindLabel ( [SURFACTANTS], [AMMONIA], [CHLORINE] ):
    surf, amm, chlor = "","",""
    if float([SURFACTANTS]) >0.24:
        surf = "Surfactants: " + [SURFACTANTS] + "\n"
    if float([AMMONIA]) >= 0.5:
         amm = "Ammonia: " + [AMMONIA] + "\n"
    if float([CHLORINE]) >= 0.02:
         chlor = "Chlorine: " + [CHLORINE]
    return surf + amm + chlor

RhettZufelt_0-1678902633032.png

 

R_

View solution in original post

0 Kudos
9 Replies
DanPatterson
MVP Esteemed Contributor

If you are using a python parser and these

[SURFACTANTS], [AMMONIA], [CHLORINE]

are supposed to be field names, you need to replace [] with surrounding ! !


... sort of retired...
AnthonyBomba
Emerging Contributor

I am using a python parser and I just included surrounding ! ! but I still seem to be getting the same error message. One additional thing to note is that in previous versions the "def" in def FindLabel was highlighted blue (assuming it's a function), but that doesn't happen in the current version. Could the issue in line 1 be there?

0 Kudos
RhettZufelt
MVP Notable Contributor

along with the fields as @DanPatterson  mentioned, it appears as if it has lost it's indentation (at least in the pasted example).

Need to indent everything below line one.

 

R_

0 Kudos
AnthonyBomba
Emerging Contributor

Thank you for the help @RhettZufelt and @DanPatterson . Here is what the code looks like now

def FindLabel ( !SURFACTANTS!, !AMMONIA!, !CHLORINE! ):
    surf, amm, chlor = "","",""
    if float(!SURFACTANTS!) >0.24:
        surf = "Surfactants: " + !SURFACTANTS! + "\n"
    if float(!AMMONIA!) >= 0.5:
        amm = "Ammonia: " + !AMMONIA! + "\n"   
    if float(!CHLORINE!) >= 0.02:
        chlor = "Chlorine: " + !CHLORINE!

    return surf +  amm + chlor

I still seem to be getting the same error message though. 

0 Kudos
RhettZufelt
MVP Notable Contributor

Well, it appears as if Pro labeling likes the [] instead of !! for the field names, However, the only  way I get the same error message is if I fail to switch the language to Python "before" I paste in the code.

def FindLabel ( [SURFACTANTS], [AMMONIA], [CHLORINE] ):
    surf, amm, chlor = "","",""
    if float([SURFACTANTS]) >0.24:
        surf = "Surfactants: " + [SURFACTANTS] + "\n"
    if float([AMMONIA]) >= 0.5:
         amm = "Ammonia: " + [AMMONIA] + "\n"
    if float([CHLORINE]) >= 0.02:
         chlor = "Chlorine: " + [CHLORINE]
    return surf + amm + chlor

RhettZufelt_0-1678902633032.png

 

R_

0 Kudos
SLouq
by MVP Regular Contributor
MVP Regular Contributor

I'm no Python expert but I have used expressions for labeling and I have used the FindLabel function like you shared in your graphic. To my knowledge, that isn't the syntax for python. That is the syntax for VBScript.

0 Kudos
AnthonyBomba
Emerging Contributor

This fixed it! I'm glad it was something small like that and not a syntax error. Appreciate the help!

0 Kudos
RhettZufelt
MVP Notable Contributor

I thought that the variables were being called with VBScript syntax, but according to the documenation, and the fact that clicking/building it in Pro's label editor populates it this way, I tried it and it works.  It appears as if the python syntax/variable delineators are different in label expressions vs calculate epxressions (where fields are referenced with !field!).

R_

0 Kudos
SLouq
by MVP Regular Contributor
MVP Regular Contributor

glad it worked!

0 Kudos