Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
3  
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 Apr 19 '11 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 Apr 19 '11 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 Apr 20 '11 at 0:19
add comment

2 Answers 2

up vote 10 down vote accepted

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
share|improve this answer
4  
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. –  Jason Scheirer Apr 18 '11 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 Apr 19 '11 at 13:23
    
You still have a !field! in your code block. –  Jason Scheirer Apr 19 '11 at 18:54
    
@Jason I finally understand! Thanks –  djq Apr 19 '11 at 19:19
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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