I have a problem with the whereclause
on python for arcpy.Select_analysis
.
I try to use a variable for condition:
for row_stat in rows_stat: % for calculated the variable"maxi"
maxi= row_stat.MAX_GRID_CODE % for calculated the variable"maxi"
del rows_stat % for calculated the variable"maxi"
where = '"GRID_CODE" = maxi' % whereclause
arcpy.Select_analysis("point_acc.shp", "point_max.shp", where)
Ofcourse, I get an error message telling me that 'maxi' can't be found in "point_acc.shp" but I don't want 'maxi' but the number in the variable maxi!
Who can help me write this?
1 Answer 1
You are passing maxi
in as part of a string instead of as a variable, so the SQL statement always ends up being "GRID_CODE" = maxi
instead of variable, e.g. "GRID_CODE" = 500
when maxi = 500
.
Instead, use:
where = '"GRID_CODE" = {}'.format(maxi)
This inserts whatever value maxi
is holding into the string, and your SQL should then correctly select.
-
Finally I have still a problem when I use your code. The problem is:
ValueError: zero length field name in format
But when i ask to print my variablemaxi
i have the right numeric value.paul– paul2014年09月08日 14:33:49 +00:00Commented Sep 8, 2014 at 14:33 -
Try
maxi.getOutput(0)
; if that doesn't work, then you may need to ask a new question with a more complete snippet of your current code.Erica– Erica2014年09月08日 14:46:51 +00:00Commented Sep 8, 2014 at 14:46
for
loop?maxi
value. Is this what you want (unless your table is a table of 1 row)? If not then you need to test isMAX_GRID_CODE
is greater than themaxi
value.