Select to view content in your preferred language

Calculate field with Python codeblock

542
4
09-29-2010 10:52 AM
deleted-user-rQoEFM5qzbHE
Deactivated User
I am attempting to calculate a field in a parent join layer using a field in the child join table. I am getting the following error:

[INDENT]<class 'arcgisscripting.ExecuteError'>: ERROR 000539: Error running expression: getSchool() <type 'exceptions.TypeError'>: getSchool() takes exactly 1 argument (0 given)
Failed to execute (CalculateField).[/INDENT]

The funny thing is, that it appears to work. Unfortunately, since I get this error the rest of the script doesn't run. Here is the code for the CalculateField line:

gp.CalculateField_management("PS_lyr_out", "ScenarioTemplate_1011_temp.ES_06", 
"getSchool(!NAME!)", "PYTHON_9.3", 
"def getSchool(school):\\n  if (school <> \"NULL\"):\\n    return school\\n")


Thanks.
0 Kudos
4 Replies
JoeVondracek
Regular Contributor
Does your parameter in the call to getSchool need to have the join table name in it?  Something like "getSchool(!JoinTable.NAME!)"?
0 Kudos
deleted-user-rQoEFM5qzbHE
Deactivated User
Yeah, I realized that after I submitted my question. I also was struggling to figure out how to set up the code block, but have since figured that out. Thanks for the reply.
0 Kudos
DaleHoneycutt
Deactivated User
I, too, have a hard time figuring out the correct scripting syntax for things like Calculate Field.  The foolproof way is to execute the tool using its dialog then open the Results window (10.0) or the Results tab (9.3), right-click the result and click "Copy as Python snippet".  This copies the code snippet to the clipboard and you can then paste it into your script. 

Executing the tool to get the code snippet sort of defeats the purpose of writing a script (you just executed the tool in order to figure out how to execute the tool).  So I usually execute the tool on a toy dataset in order to get the snippet.
0 Kudos
RhettZufelt
MVP Notable Contributor
I, too, have a hard time figuring out the correct scripting syntax for things like Calculate Field.  The foolproof way is to execute the tool using its dialog then open the Results window (10.0) or the Results tab (9.3), right-click the result and click "Copy as Python snippet".  This copies the code snippet to the clipboard and you can then paste it into your script. 

Executing the tool to get the code snippet sort of defeats the purpose of writing a script (you just executed the tool in order to figure out how to execute the tool).  So I usually execute the tool on a toy dataset in order to get the snippet.


Would be nice if this actually worked.  I have tried it on several python snippets to no avail.

As an example, the script located here: http://forums.arcgis.com/threads/2937-auto-sequential-numbers-with-python?p=62103#post62103  works perfectly from the calculator (or calculate fields) tool, but will not run as a python script.  I have exported to python as well as the copy to python listed here, and the syntax is not correct.

R_

Professional ESRI beta tester
0 Kudos