Hello. I have a python code in the ArcMap field calculator that translates numbers in the Id-column to text in another column in the same attribute table. In the pre-logic-script It looks like this:
def reclass(Id):
if (Id == 0):
return "AAA"
if (Id == 1):
return "BBB"
if (Id == 2):
return "CCC"
if (Id == 3):
return "DDD"
if (Id == 4):
return "EEE"
if (Id == 5):
return "FFF"
if (Id == 6):
return "GGG"
else:
return "<Null>"
and in the other box:
reclass(!Id!)
The problem is that this code disapears every time I shut down arcMap, and have to be re-loaded when I add anything. For example if I add an object with id-class 2 it doesn't automatically writes CCC in the new column iuntill I have re-runed the script.
Is there some way I can automatically run this script as soon as I open / change anything the attribute table?
For ArcMap, there's the Attribute Assistant.
For ArcGIS Pro, there are Attribute Rules.
Attribute Rules in ArcGIS Pro is the best solution here. I will add that you can write a Python script that to perform the calculate field operation on your data and schedule it to run as a task. So every night, or even every hour, the script could run to calculate the values.
Two tips for you: using "<Null>" will enter it as a string and not a true Null.
You can avoid chaining if statements by using a switch case (dictionary lookup) construct.
def reclass(Id):
classDict = {
0: "AAA",
1: "BBB",
2: "CCC",
3: "DDD",
4: "EEE",
5: "FFF",
6: "GGG"}
return classDict.get(Id) # <- returns the value if found in the dictionary or the default true Null
#return classDict.get(Id, '<Null>') # <- returns the value if found in the dictionary or the string <Null> as the default