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
Solved! Go to Solution.
thanks for your reply Dan,
but when i try this code it's doesn't work to. i've got a problem whith my code_block..
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: ERROR 160706: Cannot acquire a lock.
Échec de l’exécution de (CalculateField).
i try to understand where is the problem but when i saw the esri explain i don't see
so it's like this:
# coding: utf-8
import arcpy
arcpy.env.overwriteOutput = True
# Recherche de la gdb pour correction
ma_table = r"C:\rvierge.gdb\proprio"
# 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,
field_type='TEXT'
)
print("CalculateField ok")
hi ! i come back to you because now when i execute the script that's erase all of my field...
i just wanna change the nuber of certain lines of my field not erase...
do you now why the script does that?
coding: utf-8
import arcpy
arcpy.env.overwriteOutput = True
# Recherche de la gdb pour correction
ma_table = r"C:\representation_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 (param_ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU"):
return (param_ccogrm == 7)
"""
# return None
arcpy.CalculateField_management(
in_table=ma_table,
field="ccogrm",
expression=expression,
expression_type="PYTHON3",
code_block=code_block,
field_type="TEXT"
)
print("CalculateField ok")
return (param_ccogrm == 7)
should just be:
return 7
unless your field is of text type. In which case it should be:
return str(7)
Also worth checking to make sure your conditions are correct.
thank for your help effectively it's all in Text type so i need to put str in my codeblock?
like:
code_block = """
def getClass(param_ccogrm, param_ddenom):
if str(param_ccogrm == 1) and str(param_ddenom == "COPROPRIETAIRES DU LOTISSEMENT LE COTEAU"):
return str(7)
else:
return None
"""
ok it'work ! but now it's change all of my ccogrm part with the number 7