3

I want to do a multiple value selection in this query with an parameter input. The parameter Regiao_Estado gets a value like the example: [AC, DF] put in the line from the script:

lyr.definitionQuery = 'UF IN' '('Regiao_Estado')'

the whole code is here. The parameter Tabela1 only exists to get the value for the field Ano. The script will do a query to select values and after that get the sum of the field. For one value it`s ok, But I want for multiple values and maybe multiple fields selection.

Layer1 = arcpy.GetParameterAsText(0)
Tabela1 = arcpy.GetParameterAsText(1)
Regiao_Estado = arcpy.GetParameterAsText(2)
Ano = arcpy.GetParameterAsText(3) 
mxd = arcpy.mapping.MapDocument("CURRENT")  

for lyr in arcpy.mapping.ListLayers(mxd):  
    lyr.name = Layer1
    lyr.definitionQuery = 'UF IN' Regiao_Estado
    arcpy.Statistics_analysis(Layer1, "F:\AgroBD.gdb\Output1", [[Ano, "SUM"]], "")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()

I get the error:

SyntaxError: invalid syntax (M6a.py, line 10)
Failed to execute (Consulta).

I tried many ways to solve this problem but don`t get any progress.

1 Answer 1

2

You havent created the SQL query properly.

Try:

lyr.definitionQuery = """UF IN ({})""".format(Regiao_Estado)

EDIT:

If you need to expand the query, try the below. This using python string formatting. Please see this link for more info.

lyr.definitionQuery = """UF IN ({0}) OR UF IN ({1})""".format(Regiao_Estado, Test)
5
  • Macro, It`s work well, But I need to remove the two single ('). I can use this format with two fields too? Like UF and another ''TEST' field with another values.
    – vcruvinelr
    Jul 10, 2017 at 12:42
  • This definition query will select multiple attributes in the UF field. If you need to select from a different field, you need to change the definition query and add an "OR" operator. for example UF IN (...) OR UF IN (...).
    – MacroZED
    Jul 10, 2017 at 12:48
  • Nice, but I can use the same format? see the example: """UF IN ({})""".format(Regiao_Estado) AND """UF IN ({})""".format(TEST)
    – vcruvinelr
    Jul 10, 2017 at 12:50
  • See edit above.
    – MacroZED
    Jul 10, 2017 at 12:51
  • MacroZED, another question. Do you see any possible to do a multivalue selection in arcpy.Statistics_analysis(Layer1, "F:\AgroBD.gdb\Output1", [[Ano, "SUM"]], "")? I will choose more than one field to calculate, But I get an error, even using arcpy.GetParameterAsText(3).split(";") for the parameter.
    – vcruvinelr
    Jul 10, 2017 at 13:11

Your Answer

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

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