1

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?

  • Could you please add more context? For example, what are you iterating and why in the for loop? – Aaron Sep 4 '14 at 15:11
  • with the 'for' , I am iterating a number (reel double) contain in an other table. I use that to find the Value Max in this other table. – paul Sep 4 '14 at 15:18
  • Looking at the logic of this code the for loop will constantly overwrite the maxi value. Is this what you want (unless your table is a table of 1 row)? If not then you need to test is MAX_GRID_CODE is greater than the maxi value. – Hornbydd Sep 4 '14 at 15:28
  • yes this table have only 1 row. It contains the value maximum of a shape producted after a arcpy.Statistics_analysis. – paul Sep 4 '14 at 15:42
2

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 variable maxi i have the right numeric value. – paul Sep 8 '14 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 Sep 8 '14 at 14:46

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.