1

I`m trying to select many values from the parameter "Estado" which will be use in the query in the code. This parameter has a value list, string type.

I think it`s a format() problem...But also I will have problems to put in the SQL expression format: 'VALUE1','VALUE2'.

I tried with .split(';'), but do not work.

import arcpy

Layer1 = arcpy.GetParameterAsText(0) 
Tabela1 = arcpy.GetParameterAsText(1) 
Estado = arcpy.GetParameterAsText(2).split(';') 
Ano = arcpy.GetParameterAsText(3).split(';')  
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, "")        
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

When I tried to run the script, I got this error:

Traceback (most recent call last):
  File "F:\AgroBD.gdb\M6a.py", line 19, 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: ERROR 999999: Error executing function.
An invalid SQL statement was used.
An invalid SQL statement was used. [Brasil_Municipios]
An invalid SQL statement was used. [SELECT * FROM Brasil_Municipios WHERE UF 
IN ([u'AC', u'AM'])]
An invalid SQL statement was used.
An invalid SQL statement was used. [Brasil_Municipios]
An invalid SQL statement was used. [SELECT OBJECTID FROM Brasil_Municipios 
WHERE UF IN ([u'AC', u'AM'])]
The table was not found. [out3]
Failed to execute (Statistics).

Failed to execute (Script1).

1 Answer 1

1

Python's split method turns a string into an array for your variable Estado.

When you call String.format in python, it simply uses the built-in array to string conversion, which formats Estado as [u'AC', u'AM']. The square brackets and the u character are likely what's causing you problems.

Instead of relying on python's built-in array to string conversion (implicitly called by format()), you should use join() to control how the string is created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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