1

I have looked at Including variable in where clause of arcpy.Select_analysis()? and while it is very similar to the question I'm asking I ran into the same issue after implementing its syntax. I feel like I'm still missing something related to how to implement the correct syntax for the below.

where_query = '"field_name" <= ' "{0}.format(year_range)"

I'm trying to make what into a geoprocessing tool that can be reused in ArcMap, but I'm receiving the following error. It looks like an issue with the way that I'm formatting the where_query variable, but I can't seem to get it right.

Traceback (most recent call last):
 File "C:\ArcPy\mean_center_drift.py", line 19, in <module>
 arcpy.Select_analysis(in_feature, year_out_name, where_query)
 File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\analysis.py", line 84, in Select
 raise e
ExecuteError: ERROR 000358: Invalid expression 1
Failed to execute (Select).

and here is the code in its current form.

import os
import arcpy
in_feature = arcpy.GetParameterAsText(0)
out_features = arcpy.GetParameterAsText(1)
origin_year = arcpy.GetParameterAsText(2)
field_name = arcpy.GetParameterAsText(3)
for x in range(10, 140, 10):
 year_range = int(origin_year) + x
 where_query = '"field_name" <= ' "{0}.format(year_range)"
 year_out_name = os.path.join(out_features, "Years_{0}".format(x))
 mean_out_name = os.path.join(out_features, "Mean_{0}".format(x))
 arcpy.Select_analysis(in_feature, year_out_name, where_query)
 arcpy.MeanCenter_stats(year_out_name, mean_out_name, "#", "#", "#")
3
  • I realize that the general question and solution are the same as the post you linked. However, I'm using a different set of arguments and was unable to replicate the solution in the link provided. I was looking for some more specific instruction for my exact issue. Commented Aug 6, 2015 at 18:57
  • Take a look at blah238's solution in the provide link. It is the more appropriate approach. It not only takes care of field delimiters, but it also allows you to have a variable on both sides of the where clause. Commented Aug 6, 2015 at 19:03
  • Don't cross-post questions Commented Aug 8, 2015 at 1:57

1 Answer 1

2

This looks familiar! Depending on where your data is stored, the formatting of field_name in the WHERE clause will vary.

So, you can use AddFieldDelimiters to solve this for you.

import os
import arcpy
in_feature = arcpy.GetParameterAsText(0)
out_features = arcpy.GetParameterAsText(1)
origin_year = arcpy.GetParameterAsText(2)
field_name = arcpy.GetParameterAsText(3)
path = arcpy.Describe(in_feature).path
new_field_name = arcpy.AddFieldDelimiters(path, field_name)
for x in range(10, 140, 10):
 year_range = int(origin_year) + x
 where_query = """{0} <= {1}""".format(new_field_name, year_range)
 year_out_name = os.path.join(out_features, "Years_{0}".format(x))
 mean_out_name = os.path.join(out_features, "Mean_{0}".format(x))
 arcpy.Select_analysis(in_feature, year_out_name, where_query)
 arcpy.MeanCenter_stats(year_out_name, mean_out_name, "#", "#", "#")
answered Aug 6, 2015 at 19:06
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.