Select to view content in your preferred language

Calculate Field VB to Python

551
3
03-06-2023 12:42 PM
ChristianRowe
New Contributor

I have a VB script used for caluculating field data, but need it in Python. Here is the VB Script:

 

Dim a

If ( [DA_LU_SOIL.SOIL] ="B") Then

a= [CN_Reference.B]

elseif ( [DA_LU_SOIL.SOIL] ="C") Then

a= [CN_Reference.C]

elseif ( [DA_LU_SOIL.SOIL] ="D") Then

a= [CN_Reference.D]

end if

 

0 Kudos
3 Replies
by Anonymous User
Not applicable

Here are two ways- not sure what your fields are so replace them as needed:

if DA_LU_SOIL.SOIL =="B":
    a = CN_Reference.B
elif DA_LU_SOIL.SOIL == "C":
    a = CN_Reference.C
elif DA_LU_SOIL.SOIL == "D":
    a = CN_Reference.D
# or

valDict = {"B": CN_Reference.B,
           "C": CN_Reference.C,
           "D": CN_Reference.D}

a = valDict.get(DA_LU_SOIL.SOIL)
0 Kudos
ChristianRowe
New Contributor

ChristianRowe_0-1678197516963.png

You helped get me closer but now I'm getting this error, any tips?

 

Thanks a ton.

0 Kudos
by Anonymous User
Not applicable

calculate-field-examples - Check out the Calculate fields using logic section.

 

In the first block (expression) you need to call the function that you define in the Code Block.

SoilsUnion.CN =

calcfunction(!Soils.Union3_HSG!, !CN_Reference.A!, !CN_Reference.B!, !CN_Reference.C!, !CN_Reference.D!)

(you may need to remove the !.  Easiest way is to just double click on the field and Pro will put it in the argument with the right decoration.)

Code Block:

def calcfunction(checkField, refA, refB, refC, refD):
    if checkField =="A":
        return refA
    elif checkField =="B":
        return refB
    elif checkField == "C":
        return refC
    elif checkField == "D":
        return refD

 

0 Kudos