I made this script to calculate the sum for a selected field after a query definition is defined. I have some issues now, to do with a multivalue field. The code is:

import arcpy

Layer1 = arcpy.GetParameterAsText(0) 
Tabela1 = arcpy.GetParameterAsText(1) 
Estado = arcpy.GetParameterAsText(2)  
Ano = arcpy.GetParameterAsText(3)  
mxd = arcpy.mapping.MapDocument("CURRENT")  
Saida = arcpy.GetParameterAsText(4)
stats = []

for lyr in arcpy.mapping.ListLayers(mxd):   
    lyr.name = Layer1
    lyr.definitionQuery = """UF IN ({})""".format(Estado)

for field in arcpy.ListFields(Tabela1):
    if field.name in (Ano):
        stats.append([field.name, "SUM"])
        arcpy.Statistics_analysis(Layer1, Saida, stats, "")

I entered a multivalue parameter but the output show me only one field sum. The other problem is even table do not exist the console show me this error:

Traceback (most recent call last):
  File "F:\AgroBD.gdb\M6a.py", line 18, in <module>
    arcpy.Statistics_analysis(Layer1, Saida, stats, "")
  File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line      1160, in Statistics
    raise e
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000725: Output Table: Dataset F:\AgroBD.gdb\c1 already exists.
Failed to execute (Statistics).

Failed to execute (Consulta).
  • Your code does not make sense, you get a lyr set it's name to Layer1 then run the statistic on Layer1, but all layers are named Layer1...? – Hornbydd Jul 10 '17 at 13:57
  • Yes it`s works well. The problem in this code is when I put a parameter with multivalue and how to do the arcpy.Statistics_analysis with a multivalue field. – Vinicius Cruvinel Jul 10 '17 at 14:00
  • Also you need to expand out your LIST Ano into a list of lists. Split() returns a LIST. – Hornbydd Jul 10 '17 at 14:00
  • How can I do this? – Vinicius Cruvinel Jul 10 '17 at 14:01
  • As always look at the help! Code Sample 3 shows you how to build the parameter – Hornbydd Jul 10 '17 at 14:03

Your Answer

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

Browse other questions tagged or ask your own question.