0

I add a parameter CampoConfere = arcpy.GetParameterAsText(1) which get a field from Tabela_Base to calculate in the next steps from the code. The Scripts works well until the Calculate Field Step when the prompt says that "CampoConfere" is not valid. Why Python do not calculate this parameter?

import arcpy

Tabela_Base = arcpy.GetParameterAsText(0)
CampoConfere = arcpy.GetParameterAsText(1)
arcpy.AddMessage('Tabela de Entrada: '+Tabela_Base)
arcpy.AddMessage('Campo: '+CampoConfere)
fields = arcpy.ListFields(Tabela_Base)
ValidaCampo = sum(field.name == CampoConfere and field.type == "Single" for field in fields)

if not ValidaCampo:

    Nome_Campo = "CHAVEIBGE2" 
    Tipo_Campo = "FLOAT" 
    Campo_Calculo = "CHAVEIBGE2"
    Saida1 = Tabela_Base

arcpy.AddField_management(Tabela_Base, Nome_Campo, Tipo_Campo, "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.CalculateField_management(Saida1, Campo_Calculo, "!CampoConfere!", "PYTHON_9.3", "")

Them I get the message error:

Executing: Script F:\AgroDB.gdb\IBGE\Brasil_Municipios COD_IBGE Start Time: Fri Jun 23 08:46:05 2017 Running script Script... Tabela de Entrada: F:\AgroDB.gdb\IBGE\Brasil_Municipios Tabela de Entrada: COD_IBGE Failed script Script...

Traceback (most recent call last): File "F:\AgroDB.gdb\Modelo1.py", line 18, in arcpy.CalculateField_management(Saida1, Campo_Calculo, "!CampoConfere!", >"PYTHON_9.3", "") File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\management.py", >line 3453, in CalculateField raise e ExecuteError: ERROR 000539: Invalid field CampoConfere Failed to execute (CalculateField).

Failed to execute (Script). Failed at Fri Jun 23 08:46:06 2017 (Elapsed Time: 0,74 seconds)

  • 2
    That variable is not in scope for field calculator. After you've chosen a value it is essentially a constant, try specifying like "!{}!".format(CampoConfere) to substitute the value of the variable rather than the variable name. – Michael Stimson Jun 23 '17 at 4:25
  • 1
    I see that you have output arcpy.AddMessage('Tabela de Entrada: '+Tabela_Base) but I think it would also be useful to output a value coming from your tool dialog for CampoConfere, and then to show us what you get in the Results window as text. – PolyGeo Jun 23 '17 at 7:45
  • @PolyGeo when I put another arcpy.AddMessage line, this is the output: "Executing: Script F:\AgroDB.gdb\IBGE\Brasil_Municipios COD_IBGE Start Time: Fri Jun 23 08:16:12 2017 Running script Script... Tabela de Entrada: F:\AgroDB.gdb\IBGE\Brasil_Municipios Tabela de Entrada: COD_IBGE" Failed script Script... the field name is right "COD_IBGE" but the python do not calculate the fields for the new column. – Vinicius Cruvinel Jun 23 '17 at 11:16
  • @MichaelMiles-Stimson the "{}" didnt working and i didn`t understand where I use your tip – Vinicius Cruvinel Jun 23 '17 at 11:18
  • Always put those output messages into your question body, and also update the code there do that we can see precisely what you ran and saw. – PolyGeo Jun 23 '17 at 11:19
3

Try using Python String Formatting to insert your variable into a string.

For example, if your field name is SampleField, you could use string formatting in this line

arcpy.AddMessage('Campo: {0}'.format(CampoConfere))

to produce an output like

Campo: SampleField

You can use .format() to insert variables into any string, so you can also use it in your arcpy.CalculateField_management()

arcpy.CalculateField_management(Saida1, Campo_Calculo, "!{0}!".format(CampoConfere), "PYTHON_9.3", "")

Which, using your variables and my example field name above, would insert variables to make the actual command like:

arcpy.CalculateField_management("SampleFC", "CHAVEIBGE2", "!SampleField!", "PYTHON_9.3", "")

Additionally, if this line ValidaCampo = sum(field.name == CampoConfere and field.type == "Single" for field in fields) ever returns True then you will get errors as your last two lines are not indented into the if not ValidaCampo: block, yet they use variables only assigned within that if block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.