All the literature I have read suggests that to pass a string value to a another field in a feature class via ArcPy you need to do the following. I am running it in ArcMap 10.6.
arcpy.CalculateField_management(New_Labels_layer, "FC_Field", "'Word_Variable'", "VB", "")
will pass a word variable. While it successfully runs it does not pass the variable into the field. I did an exist function. So I know it exists and is being identified. The field I want to pass to is a string field.
arcpy.CalculateField_management(New_Labels_layer, "Area", Numeric_Variable, "VB", "")
successfully passes a numeric variable.
I have tried the following variations:
'Word_Variable'
"Word_Variable"
"'%Word_Variable%'"
"%Word_Variable%"
!Word_Variable!
None of these seems to work. Most allow me to run the script but do not pass anything.
2 Answers 2
Check the syntax for the arcpy.CalculateField()
function:
CalculateField_management(in_table, field, expression, {expression_type}, {code_block})
In the expression
parameter you have to pass a string referencing a field. However, the way you reference another field will depend on the expression_type
. In your code you are specifying VB
. In the tool's documentation is stated that:
For VB calculations, field names must be enclosed in square brackets (
[fieldname]
).
In case you want to pass a Python expression you have to specify 'PYTHON'
as your expression_type
. Furthermore:
For Python calculations, field names must be enclosed in exclamation points (
!fieldname!
).
None of your examples would work by passing a VB
expression. however, your last example should work if you change the expression_type
to 'PYTHON'
.
-
# Process: Calculate Field arcpy.CalculateField_management(New_Labels_layer, "Area", !Word_Variable!, "VB", "")
give me the following errorTraceback (most recent call last): File "C:\Documents\Type_SelectV1.py", line 208, in <module> arcpy.CalculateField_management(New_Labels_layer, "Area", [Word_Variable], 'VB', "") File "c:\program files (x86)\arcgis\desktop10.6\arcpy\arcpy\management.py", line 3654, in CalculateField raise e RuntimeError: Object: Error in executing tool Failed to execute (TypeSelections).
Damned_Novice– Damned_Novice2019年06月26日 18:15:24 +00:00Commented Jun 26, 2019 at 18:15 -
# Process: Calculate Field arcpy.CalculateField_management(New_Labels_layer, "Area", [Word_Variable], "PYTHON", "")
give me the following errorSyntaxError: invalid syntax (Type_SelectV1.py, line 208) Failed to execute (TypeSelections).
Damned_Novice– Damned_Novice2019年06月26日 18:16:01 +00:00Commented Jun 26, 2019 at 18:16 -
Yeah, you are mixing it up. For
'PYTHON'
use'!Word_Variable!'
, for'VB'
use'[Word_Variable]'
Marcelo Villa– Marcelo Villa2019年06月26日 19:09:45 +00:00Commented Jun 26, 2019 at 19:09 -
1Furthemore, make sure you are passing a string. If you pass
[Word_Variable]
Python will interpret it a list.Marcelo Villa– Marcelo Villa2019年06月26日 19:20:41 +00:00Commented Jun 26, 2019 at 19:20 -
1It is working fine for me. The only thing that occurs to me is that you declared
Word_Variable
somewhere in your script and it is a string. If the field you are trying to calculate is calledArea
and your text field is calledmy_field
the code would be as follows:arcpy.CalculateField_management(New_Labels_Layer, 'Area', '!my_field!', 'PYTHON')
Marcelo Villa– Marcelo Villa2019年06月26日 21:01:05 +00:00Commented Jun 26, 2019 at 21:01
When you put the name of your variable into quotes, you are essentially just passing the name of the variable as a string. What you want to do is pass the variable itself, as it is a reference to a string.
Do it just like you do for the numeric variable and pass the variable name without quotes and it should work as expected.
arcpy.CalculateField_management(New_Labels_layer, "FC_Field", Word_Variable, "VB", "")