I have an expression in VBscript that works well for labeling:
Function FindLabel (field)
if (INT(RIGHT(field, 2)) <> 1) then
FindLabel = INT(MID(field, 8, 3)) &"-"& INT(RIGHT(field,2))
else
FindLabel = INT(MID(field, 8, 3))
end if
End Function
This expression labels a text in this format: VCC065-00102 to: 1-2 or from VCC065-00501 to: 5 (using just the last 5 carachters, if the last one is> 1, it shows the number after a "-").
But, when I tried to use the same script for calculating a field, I've got a syntax error error:
Code block: the same as above
Expression:
FindLabel ([DATA])
Also, I tried to convert this script to Python:
Code Block:
def FindLabel (Field):
Var1 = Field[-2:]
Var2 = Field[8:-3]
if (int(Var1) <> 1 ):
FindLabel = int(Var2) &"-"& int(Var1)
else:
FindLabel = int(Var2)
Expression:
FindLabel (!DATA!)
But it returns an error: The field is not nullable. (I don't know why, but the expression is returning a null value)
Thanks!
-
Have you tried selecting just a single record and running your script above to confirm it works? If you run the script on the entire dataset, and have even one record where !DATA! is blank or null, it could throw this error if the output field is not nullable.RyanKDalton– RyanKDalton2014年04月24日 14:25:00 +00:00Commented Apr 24, 2014 at 14:25
2 Answers 2
Your Python code isn't actually returning a value, so that's why you are getting the not nullable error.
You need to use the return
statement instead. Additionally, use +
to concatenate strings instead of &
:
def FindLabel (Field):
Var1 = Field[-2:]
Var2 = Field[8:-3]
if (int(Var1) <> 1 ):
return str(int(Var2)) + "-" + str(int(Var1))
else:
return str(int(Var2))
Thanks a lot! Problem solved. Also, I found other errors (on slicing and I needed to convert to string to concatenate):
def FindLabel (field):
Var1 = field [-2:]
Var2 = field [7:10]
A = int(Var1)
B = int(Var2)
if (int(Var1) <> 1 ):
return str(B) + "-" + str(A)
else:
return str(B)
-
I think you should Accept Answer from @EvilGenius but leave this one here too2014年04月24日 21:27:42 +00:00Commented Apr 24, 2014 at 21:27
Explore related questions
See similar questions with these tags.