CalculateField_management

995
15
Jump to solution
07-12-2021 02:03 AM
danielROUFFART1
New Contributor II

Hi everybody,

i need some help about the arcpy expression CalculateField_management.

So wrote a py program to correct some field in a layer.

so wrote my py prog like this


import arcpy


# Recherche de la gdb pour correction

ma_table = R"C:\représentation parcelaire\cadpmm_vierge.gdb"

# rappatriment des champs utilises dans la fonction
expression = "ma_fonction(param_ccogrm=!ccogrm!, param_ddenom=!ddenom!)"
print("environnement charge")
# correction du champ ccogrm
code_block: str = """def ma_fonction(param_ccogrm, param_ddenom):
if param_ccogrm == 1 and ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU":
return 7
return None"""
arcpy.CalculateField_management(
in_table=ma_table,
field="ccogrm",
expression=expression,
expression_type="PYTHON3",
code_block=code_block
)

 

so when i execute the program, i've got an error message

Traceback (most recent call last):
File "C:/Users/d.rouffart/PycharmProjects/pythonProject/2021_07_08_ReplaceValue.py", line 60, in <module>
code_block=code_block

 

can you help me to resolve my problem please. I don't see xhere is the problem .

 

thanks

0 Kudos
1 Solution

Accepted Solutions
HamishMorton
Esri Contributor

Hi @danielROUFFART1,

You havent specified your table in the line - it is just the path to the geodatabase:

ma_table = r"C:\représentation parcelaire\cadpmm_vierge.gdb"

 

does it work if you specify the table name?

ma_table = r"C:\représentation parcelaire\cadpmm_vierge.gdb\ma_table"

 

The formatting of the function also looks a bit strange where you have pasted it to the browser. Might it be safer to use the following?

code_block = """
def ma_fonction(param_ccogrm, param_ddenom):
   if param_ccogrm == 1 and ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU":
      return 7
   else:
      return None
"""
Hamish

View solution in original post

0 Kudos
15 Replies
DanPatterson
MVP Esteemed Contributor

 

code_block ="""
def ma_fonction(param_ccogrm, param_ddenom):
    if (param_ccogrm == 1) and (ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU"):
       return 7
    return None
"""

 

don't put str = in the code block if you did


... sort of retired...
0 Kudos
danielROUFFART1
New Contributor II

thanks for your help,

so i've retired the str in the prog and i've got the same message.

 

0 Kudos
HamishMorton
Esri Contributor

Hi @danielROUFFART1,

You havent specified your table in the line - it is just the path to the geodatabase:

ma_table = r"C:\représentation parcelaire\cadpmm_vierge.gdb"

 

does it work if you specify the table name?

ma_table = r"C:\représentation parcelaire\cadpmm_vierge.gdb\ma_table"

 

The formatting of the function also looks a bit strange where you have pasted it to the browser. Might it be safer to use the following?

code_block = """
def ma_fonction(param_ccogrm, param_ddenom):
   if param_ccogrm == 1 and ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU":
      return 7
   else:
      return None
"""
Hamish
0 Kudos
danielROUFFART1
New Contributor II

hi HamishMorton

so i try your solution and i've got thie reponse for

# coding: utf-8

import arcpy


# Recherche de la gdb pour correction

ma_table = r"C:\représentation parcelaire\cadpmm_vierge.gdb\proprio"

# rappatriment des champs utilises dans la fonction
expression = "ma_fonction(param_ccogrm=!ccogrm!, param_ddenom=!ddenom!)"
print("environnement charge")
# correction du champ ccogrm
code_block ="""
def ma_fonction(param_ccogrm, param_ddenom):
if param_ccogrm == 1 and ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU":
return 7
else:
return None
"""

arcpy.CalculateField_management(
in_table=ma_table,
field="ccogrm",
expression=expression,
expression_type="PYTHON3",
code_block=code_block
)
print("CalculateField ok")

 

and for response:

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" C:/Users/d.rouffart/PycharmProjects/pythonProject/2021_07_08_ReplaceValue.py
environnement charge
Traceback (most recent call last):
File "C:/Users/d.rouffart/PycharmProjects/pythonProject/2021_07_08_ReplaceValue.py", line 27, in <module>
code_block=code_block
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5209, in CalculateField
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5206, in CalculateField
retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: ERROR 160706: Cannot acquire a lock.
Échec de l’exécution de (CalculateField).

 

0 Kudos
HamishMorton
Esri Contributor

Okay thats a bit better. Is the feature class open or being edited in any other app? If so try closing those and then run the script again from wherever you are running it. e.g. your IDE.

Hamish
0 Kudos
danielROUFFART1
New Contributor II

so i close arcgis pro and i try to start my py with charmPy and i've got that

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" C:/Users/d.rouffart/PycharmProjects/pythonProject/2021_07_08_ReplaceValue.py
environnement charge
Traceback (most recent call last):
File "C:/Users/d.rouffart/PycharmProjects/pythonProject/2021_07_08_ReplaceValue.py", line 21, in <module>
code_block=code_block
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5209, in CalculateField
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 5206, in CalculateField
retval = convertArcObjectToPythonObject(gp.CalculateField_management(*gp_fixargs((in_table, field, expression, expression_type, code_block, field_type), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Échec de l’exécution. Les paramètres ne sont pas valides.
ERROR 000732: Table en entrée: Dataset C:\représentation parcelaire\cadpmm_vierge.gdb\proprio does not exist or is not supported
Échec de l’exécution de (CalculateField).


Process finished with exit code 1

0 Kudos
HamishMorton
Esri Contributor

That is odd. I would make sure that your data still exists and that it is a feature class or table.

import arcpy

path = r"C:\représentation parcelaire\cadpmm_vierge.gdb\proprio"

if arcpy.Exists(path):
  print("{} exists".format(path))
  desc = arcpy.Describe(path)
  print("Data type is {}".format(desc.dataType))
else:
  print("{} does not exist".format(path))
Hamish
0 Kudos
danielROUFFART1
New Contributor II

Thanks for your help! thank of you i found the error!

i've forget an _ on the way to the gdb.

so now i've got that

"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe" C:/Users/d.rouffart/AppData/Roaming/JetBrains/PyCharmCE2021.1/scratches/scratch.py
environnement charge
CalculateField ok

Process finished with exit code 0

 

one more time thanks for your help

DanPatterson
MVP Esteemed Contributor

so it looks like this now?

import arcpy
# Recherche de la gdb pour correction
ma_table = R"C:\représentation parcelaire\cadpmm_vierge.gdb"
# rappatriment des champs utilises dans la fonction
expression = "ma_fonction(!ccogrm!, !ddenom!)"
print("environnement charge")
# correction du champ ccogrm
code_block = """
def ma_fonction(param_ccogrm, param_ddenom):
    if (param_ccogrm == 1) and (ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU"):
        return 7
    return None
"""
arcpy.CalculateField_management(
    in_table=ma_table,
    field="ccogrm",
    expression=expression,
    expression_type="PYTHON3",
    code_block=code_block
)

... sort of retired...
0 Kudos