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
Solved! Go to Solution.
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
R_
If you are using a python parser and these
[SURFACTANTS], [AMMONIA], [CHLORINE]
are supposed to be field names, you need to replace [] with surrounding ! !
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?
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_
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.
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
R_
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.
This fixed it! I'm glad it was something small like that and not a syntax error. Appreciate the help!
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_
glad it worked!