1

This question already has an answer here:

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, "#", "#", "#")

marked as duplicate by Dan C, Mapperz Aug 6 '15 at 20:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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. – Chris Rice Aug 6 '15 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. – Barbarossa Aug 6 '15 at 19:03
  • Don't cross-post questions – Mike T Aug 8 '15 at 1:57
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, "#", "#", "#")

Not the answer you're looking for? Browse other questions tagged or ask your own question.