How to run the Calculate Field using ArcPy?

1224
5
01-05-2022 04:23 AM
ShaistaBaloch
New Contributor

I have an issue in executing the Calculate field command in Python (ArcPy). I couldn't find any related resources or helpful descriptions regarding this. I hope somebody could help me with this.

inFeatures = r"H:\Python Projects\PycharmProjects\ArcPy\Test_Output\Trial_out.gdb\Trail_Data_A.shp"


arcpy.CalculateField_management(inFeatures, 'ObjArt_Ken', '!AttArt_Ken!'.split('_')[0])
arcpy.CalculateField_management(inFeatures, 'Wert', '!AttArt_Ken!'.split('_')[-1])

The error i am getting when I run the command is

arcgisscripting.ExecuteError: Error during execution. Parameters are invalid.
ERROR 000989: The CalculateField tool cannot use VB expressions for services.
Error while executing (CalculateField).

I am using ArcGIS Pro 2.8 and Python 2.7

0 Kudos
5 Replies
JohannesLindner
MVP Regular Contributor

You can find the documentation for the tool here:

https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/calculate-field.htm 

CalculateField has the following signature:

arcpy.management.CalculateField(in_table, field, expression, {expression_type}, {code_block}, {field_type}, {enforce_domains})

expression_type is an optional argument specifying the language you want to use (Python, Arcade, SQL), with Python being the default.

Now here is where it gets fishy: You said you were working with ArcGIS Pro, but the error you got says you are using VB as the language for CalculateField. This would make sense, as VB is the default value for CalculateField in ArcMap (which also uses Python 2.7, while Pro uses Python 3). Something doesn't check quite out...

There are some other problematic things with your code:

  • your in_features seem to be in a file geodatabase (gdb), but you still specified the .shp extension. don't know if that will cause an error (I hope it will).
  • the docs (see link above) specify that for Python code, you should enclose field names with !, you use '!
  • the expression  is given as a string, and your Python expression  will be wrong, as you didn't enclose it in quotes.

Try using this:

inFeatures = r"H:\Python Projects\PycharmProjects\ArcPy\Test_Output\Trial_out.gdb\Trail_Data_A" # removed the .shp

expr_1 = "!AttArt_Ken!.split('_')[0]" # note the enclosing "
expr_2 = "!AttArt_Ken!.split('_')[-1]" # note the enclosing "

# for ArcMap & Python 2.7
arcpy.CalculateField_management(inFeatures, 'ObjArt_Ken', expr_1, "PYTHON")
arcpy.CalculateField_management(inFeatures, 'Wert', expr_2, "PYTHON")

# for ArcGIS Pro and Python 3:
arcpy.CalculateField_management(inFeatures, 'ObjArt_Ken', expr_1, "PYTHON3")
arcpy.CalculateField_management(inFeatures, 'Wert', expr_2, "PYTHON3")

 


Have a great day!
Johannes
JohannesLindner
MVP Regular Contributor

For completeness, here's the doc for ArcMap:

https://desktop.arcgis.com/en/arcmap/latest/tools/data-management-toolbox/calculate-field.htm 


Have a great day!
Johannes
0 Kudos
Luke_Pinner
MVP Regular Contributor

Your file path is r"...\Trial_out.gdb\Trail_Data_A.shp"

This will never work.  You appear to have saved a shapefile into a .gdb folder that while it is just a folder to Windows, is not treated as a folder by ArcGIS, but as a file geodatabase. ArcGIS does not recognise non-file geodatabase data that is stored in the .gdb folder.

  1. Move your Trail_Data_A.shp, Trail_Data_A.dbf, Trail_Data_A.shx, Trail_Data_A.* files out of the Trial_out.gdb folder to a normal folder.
  2. If you want a file geodatabase feature class, use ArcGIS to export your shapefile to the database.

 

MalikaCheema
New Contributor

I found a working output for the above mentioned question

inTable = r'H:\Python Projects\PycharmProjects\ArcPy\Test_Output\Trial_out.gdb\Trail_Data_A.shp'

func_1 = "'!AttArt_Ken!'.split('_')[0]"
field_1 = "ObjArt_Ken"

func_2 = "'!AttArt_Ken!'.split('_')[1]"
field_2 = "Wert"

arcpy.AddField_management(inTable, field_1, "TEXT".encode('utf-8'))
arcpy.CalculateField_management(inTable, field_1, func_1, "PYTHON_9.3")

arcpy.AddField_management(inTable, field_2, "TEXT".encode('utf-8'))
arcpy.CalculateField_management(inTable, field_2, func_2, "PYTHON_9.3")

But a problem still exists such that the output (After split function) is like |ObjArt_Ken|Wert| |:---|:---| |u"31001|1001"|

I still couldn't figure out a way to remove the unicode or the '"' symbol from the desired output

0 Kudos
JohannesLindner
MVP Regular Contributor

Seems like you saved the unicode symbol and the double quotes as actual text.

Try this:

# define a function that will convert 'u"1234_5678"' to '1234_5678'
# CalculateField needs it as string
code_block = """
def clean_up(text_value):
    if text_value.startswith('u"'):
        text_value = text_value[1:]  # remove unicode symbol
    return text_value.replace('"', '')  # remove double quotes
"""

func_1 = "clean_up(!AttArt_Ken!).split('_')[0]"  # without single quotes around the field name!
field_1 = "ObjArt_Ken"

func_2 = "clean_up(!AttArt_Ken!).split('_')[1]" # without single quotes around the field name!
field_2 = "Wert"

arcpy.AddField_management(inTable, field_1, "TEXT".encode('utf-8'))
arcpy.CalculateField_management(inTable, field_1, func_1, "PYTHON_9.3", code_block)

arcpy.AddField_management(inTable, field_2, "TEXT".encode('utf-8'))
arcpy.CalculateField_management(inTable, field_2, func_2, "PYTHON_9.3", code_block)

Have a great day!
Johannes
0 Kudos