8

I'm trying to used the Python syntax in ArcGIS 10's field calculator and am thoroughly confused. So far the most basic calculations are failing me, and I don't understand why. I'm trying to create a function to just set the values of all cells in a column to be a multiple of the argument that I pass in ( I wanted to try something simple to test it out, as anything more complicated I tried also failed). enter image description here

However, the error messages are telling me that my syntax is incorrect:

enter image description here

but I can't understand what is incorrect.


As an aside, I'm also horrified at how hard it is to write readable python in the field calculator box as there is no syntax highlighting, or clear indentation. It would be quicker, and easier to write an external Python script to do this calculation, but I want to try to figure out how this new function worked within the program.

3
  • 4
    I sympathize with your complaint about the lack of syntax highlighting. I'll also add to that that the textbox uses a variable width font (rather than a fixed width font). As far as I'm concerned, it's just a place to paste code developed and tested elsewhere (I use PythonWin).
    – Mike T
    Commented Apr 19, 2011 at 2:07
  • 1
    @Mike How do you deal with testing variable using the existing data/variables? I normally write python scripts that are completely external (using Geany). I was hoping not to even have to write functions in the code-block; I just wanted a quick way to do a more complicated calculation than a + b
    – djq
    Commented Apr 19, 2011 at 13:25
  • I use PythonWin for everything (from Python Win32). My workflow would be to develop a script in a file (e.g. def test(var1): return var1*2), then copy the code block and right-click in the interactive window choosing "Execute python code from clipboard", then test/debug the function with possible values for var1 (e.g., test(4) should return 8). It's a pure Python method of developing and testing the code block before introducing it to ArcGIS.
    – Mike T
    Commented Apr 20, 2011 at 0:19

2 Answers 2

12

It might be because you're using a field name where a parameter name should go.

Instead, try something like:

def avg(paramName):
    return 10
3
  • 6
    The !field! convention only works inside the expression, not in the code block. You can also just put 10 as the expression and leave the code block blank. Commented Apr 18, 2011 at 22:48
  • I've just updated my example - I'm still getting an error though. @Jason I realize I could leave the code block blank but I want to do a more complicated calculation once I understand how this works.
    – djq
    Commented Apr 19, 2011 at 13:23
  • You still have a !field! in your code block. Commented Apr 19, 2011 at 18:54
9

As Jason Scheirer commented, you can't use !field! inside of pre-script logic.

Pre Script Logic:

def demo(value, arg):
   return value * arg

avg_miles =

demo(!hshld_2000!,10)

Note: A "trick" you can use to simplify this mess... You can use the interactive Command window in ArcMap to create and test python code (such as defining methods). Once you're get things working, you can reference a python method created in the interactive window from the Calculate Field window.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.