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?

Joseph
76.7k8 gold badges173 silver badges286 bronze badges
asked Sep 4, 2014 at 14:45
4
  • Could you please add more context? For example, what are you iterating and why in the for loop? Commented Sep 4, 2014 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. Commented Sep 4, 2014 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. Commented Sep 4, 2014 at 15:28
  • yes this table have only 1 row. It contains the value maximum of a shape producted after a arcpy.Statistics_analysis. Commented Sep 4, 2014 at 15:42

1 Answer 1

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.

answered Sep 4, 2014 at 15:14
2
  • 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. Commented 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. Commented Sep 8, 2014 at 14:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.