Hello!
I'm sure this is a simple issue, but it's stumped me.
I'm new to python, please help me.
I have calculated two equations from two fields without any problem.
When I calculate the third equation using the results of the first,
an error occurs: Error 000539: Invalid Field . Failed to execute (CalculateField).
I don't know what to do in this case.
here's my code.
--------------------------------------------------------------------------
import arcpy
# Set environment settings
arcpy.env.workspace = arcpy.GetParameterAsText(0)
# arcpy.env.workspace = "D:\DocEnergy\DATA\BATIMENTS.gdb"
# ----------------------------------------
DDD = arcpy.GetParameterAsText(1).replace(',', '.')
DDDF = float(DDD)
fc = arcpy.GetParameterAsText(2)
# fields
CHAMP1 = arcpy.GetParameterAsText(3)
CHAMP2 = arcpy.GetParameterAsText(4)
# --------------------------------------------------
# EAUATION ET FORMULES
RESULTA1 = "!{0}!*!{1}!+{2}".format(CHAMP1, CHAMP2, DDDF)
RESULTA2 = "!{0}!*!{1}!-{2}".format(CHAMP1, CHAMP2, DDDF)
# how can I calculate this equation from the two equations RASULTA1 AND RESULTA2 ?
RESULTA = "!{0}!*!{1}!".format(RESULTA1, RESULTA2)
# Input the name of the field to be created
CHAMPS = arcpy.GetParameterAsText(5)
# Add field
arcpy.AddField_management("BATI", CHAMPS, "float", 20, 10)
# CalculateField
arcpy.CalculateField_management("BATI", CHAMPS, RESULTA, "PYTHON")
-------------------------------------------------------------------
thank you !!
Solved! Go to Solution.
Calculate field... missed that
code_block = """
def calc(f0, f1, f2):
a = (f0 * f1) + f2
b = (f0 * f1) - f2
return a * b"""
expression
ex = "calc(!CHAMP1!, !CHAMP2!, !DDDF!)"
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3",codeblock)
hence
arcpy.CalculateField_management("BATI", "CHAMPS", expression=ex, "PYTHON3", codeblock=code_block)
Thank you DanPatterson, your solution worked well, just need to add to the expression :
expression = "calc (!{0}!, !{1}!, {2})".format(CHAMP1, CHAMP2, DDDF )
Thank you !!!
RESULTA1 = "!{0}!*!{1}!+{2}".format(CHAMP1, CHAMP2, DDDF)
RESULTA2 = "!{0}!*!{1}!-{2}".format(CHAMP1, CHAMP2, DDDF)
# how can I calculate this equation from the two equations RASULTA1 AND RESULTA2 ?
RESULTA = "!{0}!*!{1}!".format(RESULTA1, RESULTA2)
RESULTA1 and RESULTA2 are text
hence
RESULTA = "!{0}!*!{1}!".format(float(RESULTA1), float(RESULTA2))
thank you for your feedback !
I have tested your proposal, but it gives an error:
Traceback (most recent call last):
RESULTA = "!{0}!*!{1}!".format(float(RESULTA1), float(RESULTA2))
ValueError: could not convert string to float: !U_r1!*!U_r2!+10.0
RESULTA1 = "!{0}!*!{1}!+{2}".format(CHAMP1, CHAMP2, DDDF)
RESULTA2 = "!{0}!*!{1}!-{2}".format(CHAMP1, CHAMP2, DDDF)
#RESULTA1 = '!CHAMP1!*!CHAMP2!+9.4'
#RESULTA2 = '!CHAMP1!*!CHAMP2!-9.4'
RESULTA = "({0}) * ({1})".format(RESULTA1, RESULTA2)
#'(!CHAMP1!*!CHAMP2!+9.4) * (!CHAMP1!*!CHAMP2!-9.4)'
or simply dump the extra string stuff
RESULTA1 = "5"
RESULTA2 = "4"
# -- one method
RESULTA = float(f"{RESULTA1}") * float(f"{RESULTA2}")
# -- even simpler
RESULTA = float(RESULTA1) * float(RESULTA2)
I don't know if I understood your solution correctly, I tested but it gives an error:
RESULTA = float(RESULTA1) * float(RESULTA2)
but it gives an error: File "D:\DocEnergy - copy+++++\SCRIPTE FINALLL 10-12-2021.py", line 27, in <module>
RESULTA = float(RESULTA1) * float(RESULTA2)
ValueError: could not convert string to float
t
thank you for your answer, I already tried this solution, but it gives the following error :
arcpy.CalculateField_management("BATI", CHAMPS, RESULTA, "PYTHON")
File "c:\program files\arcgis\desktop10.5\arcpy\arcpy\management.py", line 3663, in CalculateField
raise e
ExecuteError: ERROR 000539: Field CHAMP1 invalid.
Failed to execute (CalculateField).
Calculate field... missed that
code_block = """
def calc(f0, f1, f2):
a = (f0 * f1) + f2
b = (f0 * f1) - f2
return a * b"""
expression
ex = "calc(!CHAMP1!, !CHAMP2!, !DDDF!)"
arcpy.CalculateField_management(inTable, fieldName, expression, "PYTHON3",codeblock)
hence
arcpy.CalculateField_management("BATI", "CHAMPS", expression=ex, "PYTHON3", codeblock=code_block)
Thank you DanPatterson, your solution worked well, just need to add to the expression :
expression = "calc (!{0}!, !{1}!, {2})".format(CHAMP1, CHAMP2, DDDF )
Thank you !!!