Just wondering if the Python-Geoprocessing Community could help me. I am trying to write the following code block into a calculate field called weight from the values in another field called Symbol.
def Reclass( !Weight!):
if ( !SYMBOL! == 6):
return 1.2
elif ( !SYMBOL! == 85):
return 1.5
elif ( !SYMBOL! ==2):
return 1
elif ( !SYMBOL! ==42):
return 1.8
elif ( !SYMBOL! ==58):
return 2
It just won't work. help...
def Reclass( !Weight!):
if ( !SYMBOL! == 6):
return 1.2
elif ( !SYMBOL! == 85):
return 1.5
elif ( !SYMBOL! ==2):
return 1
elif ( !SYMBOL! ==42):
return 1.8
elif ( !SYMBOL! ==58):
return 2
It just won't work. help...
also any further details on the field's properties (perhaps a wrong field type, precision or scale)
This was what I was trying, but really lost track of what to put where.
Attachments
This goes in the code block:
And in the expression field you put:
I disagree. Calculate field with short snippets of python code as in this case are perfect in most situations. The one place to avoid this tool and use an update cursor instead is within a python script, becuase of the quoting issues.
But as a stand alone tool, or in a model builder model, CalculateField is a very useful tool.
If you are going the Python route, I still assert that a cursor is better because:
1. It's faster and easier to write the code (no need to post to the forums per the pain in the *** \\n\\n\\n shinanigans).
2. When you print the code it's actually understandable by a human, where as the codeblock generally comes out in one big line and is generally unintelligible.
For example (yes this is VB, but you get my drift):
It looks good, but I am still getting an error.
Here is what I put in.
ERROR
000539: Error running expression: SYMBOL_weights[2] <type 'exceptions.NameError'>: name 'SYMBOL_weights' is not defined
Failed to execute (Calculate Field).
Failed at Wed Feb 16 13:17:20 2011 (Elapsed Time: 1.00 seconds)
Attachments
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//005s0000002m000000.htm
There is an Expression box, and a Code block, in the code block there is a section for variables and/or imports to be denoted (in this case an import statement) and a Python function, as denoted by the def
If the syntax is correct and the fieldname is enclosed in double !'s then it should work
can you get this to work in a field? The error in the previous example is that SYMBOL_weights is not a function
I tested it and got the same error, and I still don't understand why. But I found an easy workaround: Rename the dictionary to something else (without underscore) like symweights. Do that both in the code block and the expression field and it works.
That works perfect. Like Python should! Amazing. I choose this method, because I wanted to use the ModelBuilder. Not sure if a cursor would work in this case.
A cursor would work just as well (untested):
Hi Ryan,
When you define the method within the Python codeblock, use a variable for the Reclass method's parameter and use that variable within the method's logic:
def Reclass(weight):
if (weight == 6):
return 1.2
elif (weight == 85):
return 85
.....
Then when you call the Reclass method in the Calculate Field Expression parameter, you will then pass in the SYMBOL field:
Reclass(!SYMBOL!)