Hello everyone
I have a question concerning Python in ArcMap:
What I want to do: I want to write a "2" in the field "Hauptgesellschaft_Gew" if the field "Hauptgesellschaft" contains the number "1235" (both fields are in the table "Wald_Poly"). This is part of an exercise given to us in which we have to calculate a suitability map.
What I have tried in the Tool "Calculate Field":
Input Table: Wald_Poly
Field Name: Hauptgesellschaft_Gew
Expression: reclass(!Hauptgesellschaft!)
Expression Type (optional): PYTHON
Code Block (optional): inTable = "Wald_Poly"
fieldName = "Hauptgesellschaft_Gew"
expression = "reclass(!Hauptgesellschaft!)"
codeblock = """def reclass(Hauptgesellschaft):
if Hauptgesellschaft == '1235':
return '2'"""
The field formats are text/string for "Hauptgesellschaft" and short integer for "Hauptgesellschaft_Gew".
Unfortunately it does not work, the programm runs, but there are no numbers written into the field "Hauptgesellschaft_Gew". We never had an introduction to Python so my (not working) solution is based on different examples I found on the internet.
The reason for this rather complicated way of solving this problem is, that I have to do everything in the model builder and that there are more complicated codes following. So if any of you know, how I could integrate an OR (like in if Hauptgesellschaft == '1235' OR Hauptgesellschaft == '1236' OR Hauptgesellschaft == '1237' ) I would be glad too.
I would be really happy, if somebody could help me. I tried for hours, but I could not get it working.
P.S. This is the first time I post something on this board, I am sorry, if I did something wrong.
Solved! Go to Solution.
you seem to be trying to use the calculate field tool, which is normally used in a script, although exclusively. I was referring to the field calculator which you access from an open table as per usual procedures
some issues... first you said the number 2 should be written into the new field... '2' is not a number but a text representation of the number.... second, you don't want anything to happen if the condition isn't met? That may be fine, it will leave null values in a geodatabase field, but it will leave 0 for shape fields.
So, check that the field type is indeed a number field (ie long, short for no decimals) or make sure it is a text field if a string.
Also, test your code block in a sample field calculation to ensure that your indentation and everything is correct, then copy and paste and enclose in the triple quotes.... use python9.3 as well
All for now
First of all, thank you very much for your answer.
"Hauptgesellschaft" is a text (so I continue writing '1235'), "Hauptgesellschaft_Gew" is a short integer (so I write 2 instead of '2')
I do want something to happen if the condition isn't met, but first I will try to understand with one condition.
What do you mean by "test your code block in a sample field calculation to ensure that your indentation and everything is correct"? How can I do that? But I am quite sure, that my indentation is NOT correct. As I said, I really have no clue at all with Python, that is why I came here, to ask what part of my code is wrong.
If I run my programm, get this error message:
ERROR 000539: Error running expression: reclass(u"1835")
Traceback (most recent call last):
File "<expression>", line 1, in <module>
NameError: name 'reclass' is not defined
Failed to execute (Calculate Field).
you seem to be trying to use the calculate field tool, which is normally used in a script, although exclusively. I was referring to the field calculator which you access from an open table as per usual procedures
This may help when trying to see the association between the field calculator expressions and how to use the CalculateField tool
Thank you very much, with your help I was able to solve this problem. One main mistake was that I did not type enough spaces in front of the "if" and "return" lines.
My final solution, just in case somebody will have the same problem in the future:
Input Table: Wald_Poly
Field Name: Hauptgesellschaft_Gew
Expression: reclass(!Hauptgesellschaft!)
Expression Type (optional): PYTHON_9.3
Code Block (optional):
def reclass(crit):
if (crit == '1'):
return 0
elif (crit == '2' or crit == '1235' or crit == '2300' or crit == '3207' or crit == '3212' or crit == '3225' or crit == '4400' or crit == '4465' or crit == '4500' or crit == '4565' or crit == '4600' or crit == '4601' or crit == '4606' or crit == '4607' or crit == '4608' or crit == '5720' or crit == '6900' or crit == '7000' or crit == '7102' or crit == '7108' or crit == '7200'):
return 2
else:
return 1
A little shorter version could be this:
def reclass(crit):
if crit == '1':
return 0
elif crit in ['2', '1235', '2300', '3207', '3212', '3225', '4400',
'4465', '4500', '4565', '4600', '4601', '4606', '4607',
'4608', '5720', '6900', '7000', '7102', '7108', '7200']:
return 2
else:
return 1