3

I am working on a toolset, and one of the inputs is a field of a shapefile. I have defined the field and I have used it in multiple functions, and it is working without any error.

The problem is that when I want to use the field in an expression in CalculateField, it does not recognize the defined field, but when I use the name of field (name in attribute table) it works.

I think that I make mistake in writing the code for the expression part.

enter image description here

import os, sys    
import arcpy
from arcpy import env
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
    
###### Define variables
shapes = sys.argv[1]
outputfolder = sys.argv[2]
outputname = sys.argv[3]
field = sys.argv[4]
output = os.path.join(outputfolder,outputname)
arcpy.AddMessage(shapes)
arcpy.AddMessage(field)
arcpy.AddMessage(output)
    
shapes1=arcpy.AddField_management(shapes,'Classific','TEXT')
shapes2=arcpy.CalculateField_management(shapes1,'Classific',"!Classific!.replace(!Classific!,field)", 'PYTHON3')
    
Unionfield = arcpy.CalculateField_management(Unionfield, field, "field+ !FIRST_Clas!", 'PYTHON3')
Unionfield = arcpy.CalculateField_management(Unionfield, field, "!Class!+ !FIRST_Clas!", 'PYTHON3')

Also, I have another question about adding the output of my toolset to the contents after each running.

When I run my toolset every time, the output does not appear in the contents, every time I have to import the output from outputfolder.

  • 2
    please ask your second question (about adding output) as a separate question. – radouxju Oct 4 at 20:18
7

You have made a basic mistake, you have embed the variable you called field directly within a text expression, i.e. within the "", so it sees it as some text not as a variable.

Do something like this:

expression = "!Classific!.replace(!Classific!,'" + field + "')"
shapes2=arcpy.CalculateField_management(shapes1,'Classific',expression, 'PYTHON3')

As a side note: shapes1, shapes2 and unionfield are RESULT objects, the result of the tool execution. I would recommend you call them something like resObj as it will help you understand your code as at the moment its completely misleading as to what they represent.

| improve this answer | |
  • Thank you so much but the expression uses the field name for replacing. I am trying to replace the field's attribute with !Classific! – Shae Oct 6 at 2:07
4

in order to use the value of a variable in a text string, I recommend to use .format().

in practice, write your string with missing parameter in between {} then list all parameters in the format function (separated by ,)

arcpy.AddField_management(shapes,'Classific','TEXT')
arcpy.CalculateField_management(shapes,'Classific',"!Classific!.replace(!Classific!,!{0}!)".format(field), 'PYTHON3')

Doing that, don't forget to add the field name between !!, otherwise it is not the value contained by the field that will be used, but its name (I don(t think that you want to copy the field name for all rows).

That being said, filling a new field with the values of another field doesn't need a complex replace statement. You could use:

arcpy.CalculateField_management(shapes,'Classific',"!{}!".format(field), 'PYTHON3')

You can apply the same method to your second example (note that the fields must be of the same type).

arcpy.CalculateField_management(shapes,field,"!{}! + !First_class! ".format(field), 'PYTHON3')
| improve this answer | |
  • I do not want to use the field's name. I want to use 'field' in expression since field is defined as input in the toolset's script. we can import any field as input using the toolset. I want to call the imported field in expression and use its attributes (not the name of the field). – Shae Oct 6 at 2:14
  • my expression does not use the field name, it uses the field's value. This is the purpose of the !! around the name – radouxju Oct 6 at 8:03
  • Thanks, for your replay. your suggestion worked in this form: arcpy.CalculateField_management(shapes,'Classific',"!{}!".format(field), 'PYTHON3') but It is not working with : arcpy.CalculateField_management(Unionfield, field, "!field!+ !FIRST_Clas!", 'PYTHON3') – Shae Oct 6 at 13:00
  • 1
    you must also insert the field name with .format , that is "!{}!+ !FIRST_Clas!".format(field) – radouxju Oct 6 at 13:47
  • Thank you so much – Shae Oct 6 at 16:09

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.