Select to view content in your preferred language

Converting VB .cal into Python for Field Calulator

967
3
07-22-2011 06:45 AM
DennisGookin
Deactivated User
My company is finally switching from ArcGIS 9.3 to ArcGIS 10.  I am testing our process and have figured out how to change simple calculations we used before into Python, but don't know enough to figure out this Else If Statement.  If someone could help it would be much appreciated.

This was in the Pre-Logic Script Code box:


Dim Class As String
Dim Output As String

Class = [FUNC_CLASS]
Output = [FCC]

If Class = "1" OR Class = "2" Then
   Output = "A15"
ElseIf Class = "3" Then
                                 Output = "A20"
ElseIf Class = "4" Then
                                 Output = "A30"
ElseIf Class = "5" Then
                                 Output = "A40"
End If



And this was in the box below with FCC= being the header (not in the box)


FCC=
Output
Tags (2)
0 Kudos
3 Replies
DarrenWiens2
MVP Alum
See this page for more examples.
Codeblock:
def func(Class):
    if Class == 1 OR Class == 2:
        return "A15"
    elif Class == 3:
        return "A20"
    elif Class == 4:
        return "A30"
    elif Class == 5:
        return "A40"

Expression:
func(!theclassfield!)
 


*I'm not sure if the final 'elif...:' must be an 'else:' instead. I can't find any examples without an 'else:' to end the 'if...:'.
0 Kudos
StacyRendall1
Frequent Contributor
Hey,

just a few notes on that:

  • Make sure your data types are correct. If the input is field actually a string, you will need to put quotes around the numbers or convert Class to an integer before the conditionals (also handy if you need to do comparative queries, < or >, for example):

    if Class == '1' ... and so on...

or

def func(Class_input):
    Class = int(Class_input)
    if Class == 1 ... and so on...


  • OR should be lower case:

 if Class == '1' or Class == '2':


  • use brackets for multiple conditional statements (not always necessary, but a good idea):

 if (Class == '1') or (Class == '2'):


  • else/elif at the end is up to the programmer, but note that else doesn't take a conditional, it just does its thing if none of the if/elif statements worked. Generally it is a good idea to have an else at the end, as it will help you identify any messy stuff with your data. For example, if 6 is passed to your code it will fail. What I often do is put else: return None at the end, which will make the field NULL... This does, however depend upon on your application, you could, for example, return 'A50'...

Codeblock:
def func(Class):
    if (Class == '1') or (Class == '2'):
        return "A15"
    elif Class == '3':
        return "A20"
    elif Class == '4':
        return "A30"
    elif Class == '5':
        return "A40"
    else:
        return None

Expression:
func(!theclassfield!)
0 Kudos
DennisGookin
Deactivated User
Thanks Stacy.  I modified everything to look like you had it and it worked.  Really appreciate the help!
0 Kudos