-1

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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jun 26, 2019 at 16:44

2 Answers 2

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'.

answered Jun 26, 2019 at 17:19
7
  • # Process: Calculate Field arcpy.CalculateField_management(New_Labels_layer, "Area", !Word_Variable!, "VB", "") give me the following error Traceback (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). Commented Jun 26, 2019 at 18:15
  • # Process: Calculate Field arcpy.CalculateField_management(New_Labels_layer, "Area", [Word_Variable], "PYTHON", "") give me the following error SyntaxError: invalid syntax (Type_SelectV1.py, line 208) Failed to execute (TypeSelections). Commented Jun 26, 2019 at 18:16
  • Yeah, you are mixing it up. For 'PYTHON' use '!Word_Variable!', for 'VB' use '[Word_Variable]' Commented Jun 26, 2019 at 19:09
  • 1
    Furthemore, make sure you are passing a string. If you pass [Word_Variable] Python will interpret it a list. Commented Jun 26, 2019 at 19:20
  • 1
    It 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 called Area and your text field is called my_field the code would be as follows: arcpy.CalculateField_management(New_Labels_Layer, 'Area', '!my_field!', 'PYTHON') Commented Jun 26, 2019 at 21:01
1

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", "")

answered Jun 26, 2019 at 17:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.