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.
2 Answers 2
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
-
6The
!field!
convention only works inside the expression, not in the code block. You can also just put10
as the expression and leave the code block blank.Jason Scheirer– Jason Scheirer2011年04月18日 22:48:47 +00:00Commented 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– djq2011年04月19日 13:23:21 +00:00Commented Apr 19, 2011 at 13:23
-
You still have a
!field!
in your code block.Jason Scheirer– Jason Scheirer2011年04月19日 18:54:47 +00:00Commented Apr 19, 2011 at 18:54
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.
Explore related questions
See similar questions with these tags.
code-block
; I just wanted a quick way to do a more complicated calculation thana + b
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 forvar1
(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.