I want to calculate a field depending on 3 input string parameters. Two of them can contain Null-values. My old VB expression was not able to work with Null-values, so the script stopped.
Now I want to develop a script which allows me to solve all my problems:
- set an if-else-condition
- calculate the field with Python expressions instead VB
- eliminate Null-values by not computing/using these cells in the expression.
This is the input table to get my parameters
And here is my script:
import arcpy, os from arcpy import env #Get Parameters importfeature = arcpy.GetParameter(0) workspace = arcpy.GetParameter(1) owner = arcpy.GetParameter(2) criteria = arcpy.GetParameter(3) keep_fieldname1 = arcpy.GetParameter(4) keep_fieldname2 = arcpy.GetParameter(5) #Set Workspace arcpy.env.workspace = workspace #Add Field arcpy.AddField_management (importfeature, criteria, "text") #Calculate field criteria if keep_fieldname1 == None and keep_fieldname2 == None: expression = '"%owner%"' elif keep_fieldname2 == None expression = '"%owner%" +" - "+ !%keep_fieldname1%!' else expression = '"%owner%" +" - "+ !%keep_fieldname1%! +" - "+ !%keep_fieldname2%!' arcpy.CalculateField_management (importfeature, criteria, expression , "Python")
I need to say that the parameters are automatically given by an iterator to the script via "get values". So don't worry about the input. This is working fine.
How do I solve my problems?