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.

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)
  • 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 '17 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 '17 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 '17 at 12:50
  • See edit above. – MacroZED Jul 10 '17 at 12:51
  • Many thanks! I will try this queries. I think it`s works well. – vcruvinelr Jul 10 '17 at 12:55

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.