Error 000539: Invalid Field . Failed execution of (CalculateField)

1897
10
Jump to solution
12-09-2021 06:05 AM
echlouchi
New Contributor II
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 !!
0 Kudos
2 Solutions

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...

View solution in original post

khalidechlouchi
New Contributor II

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 !!!

View solution in original post

0 Kudos
10 Replies
DanPatterson
MVP Esteemed Contributor

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


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

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

 

0 Kudos
JohannesLindner
MVP Frequent Contributor
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)'

Have a great day!
Johannes
0 Kudos
DanPatterson
MVP Esteemed Contributor

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)

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

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

 

0 Kudos
echlouchi
New Contributor II

t

0 Kudos
echlouchi
New Contributor II

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

0 Kudos
DanPatterson
MVP Esteemed Contributor

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)


... sort of retired...
khalidechlouchi
New Contributor II

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 !!!

0 Kudos