ArcGIS Field Calculator – How to Write Python Script in Field Calculator in ArcGIS Desktop?

arcgis-desktopfield-calculatorpython-parser

I'm a programmer, I have programmed in C#, Java, but not with Python. And I'm not a geographic worker.
How can I write a python script into Field Calculator to be able to check the other column value. If that other column value is "a certain string", the value in this column should be 0, otherwise it should be computed with an equation.
The value I want to look is a Text type, and I want to count in a Double type column.

Best Answer

You don't 'Dim' variables in python, you just declare and assign them. That said, your basic route is to set the parser to python and check Show Codeblock. In the Pre-Logic Script Code box, enter your function like this:

def DoThis(fld):
    val = 0
    if fld <> 'a certain string':
        val = # do your calculation here
    return val

In your 'field =' box, enter:

DoThis(!field!)

In python , # means a comment. Indentation counts. If your calculation depends on other fields, pass those in as well. That should be straightforward with your background. In calling the function, the field name is bracketed by exclaimation (!) marks.

enter image description here

Related Question