2

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:

  1. set an if-else-condition
  2. calculate the field with Python expressions instead VB
  3. eliminate Null-values by not computing/using these cells in the expression.

This is the input table to get my parameters

Table

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?

  • What is the output of your code? Besides the fact the formatting for the expressions might be wrong, it looks like you are not making any selections. – Jonas Aug 20 '18 at 13:51
  • The output later is a dissolve over the criteria field, which works and is not relevant for the code problem here. And if you mean with "not making selections", I have an iterator feeding this script selecting rows and getting values - it's working adequately. – Steve-O Aug 20 '18 at 13:56
  • Hey Steve-O, if you have already made the row selection, and just need to make a calculation on the fields, you could try: expression = "!{0}! + !{1}! + !{2}!".format(owner, keep_fieldname1, keep_fieldname2). this requires the input variables are fieldnames i think. – Jonas Aug 21 '18 at 6:46
1

You appear to be treating your variables in your script e.g. owner as inline model variables by surrounding them with %%. That syntax is for accessing variables in a model. You want to set your expression to something like:

expression = '"' + owner + '"'

Also set the expression type from PYTHON to PYTHON_9.3 in the Calculate Field tool.

  • Ok, I get what you mean about % and model variables. So I take them out. And I've set the calculation up to Python_9.3. But I am a bit lost to all these " and '. Can you help me creating my expression from the beginning to its end. Tried several versions now and I don't know what I'm doing wrong. – Steve-O Aug 20 '18 at 13:51
  • The variable owner contains text so is a string. When you do a field calculate you always enclose your text in quotes don't you? So your expression to be valid needs to enclose the text held in the variable in quotes. In Python to add quotes to a string you need to enclose the quotes! One approach is to do what I did single quote- double quote- single quote, that is what '"' is. – Hornbydd Aug 20 '18 at 23:07
  • Ok, your expression example works if I just enter the variable owner. But when I add a second variable - and remember that the two variables keep_fieldname1/2 are fieldnames, owner is not a fieldname - it's not working. So this is what I tried to test your idea - expression = '"' + owner + !keep_fieldname1! + !keep_fieldname1!'"' – Steve-O Aug 21 '18 at 10:32
  • You are missing a + between the last field and '"'. – Hornbydd Aug 21 '18 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.