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
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– vcruvinelr2017年07月10日 12:42:41 +00:00Commented 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– MacroZED2017年07月10日 12:48:40 +00:00Commented 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– vcruvinelr2017年07月10日 12:50:05 +00:00Commented Jul 10, 2017 at 12:50
-
-
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– vcruvinelr2017年07月10日 13:11:37 +00:00Commented Jul 10, 2017 at 13:11