I am trying to use arcpy.SelectLayerByAttribut_management using the following code:

def filterData(self):
    arcpy.MakeFeatureLayer_management (r"%s\tempclipprefilter"%self.workspace, "lyr")
    arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION", '" ATTRIBUTE" = \'self.DataQuery\' ')
    arcpy.CopyFeatures_management("lyr", self.outFC)

I have also tried just



ATTRIBUTE - self.DataQuery

in place of

'"  ATTRIBUTE" = \'self.DataQuery\' '

I have looked at Runtime error : ERROR 000358: Invalid expression using SelectLayerByAttribute in ArcPy? but this has not solved my issue.

The code producing self.DataQuery is below:

    def filterBuilder(self):
        self.queryList = []
        if self.aField == 'true':
            self.queryList.append(" myDataColumn <> 'N' ")
        if self.bField == 'true':
            self.queryList.append(" class LIKE 'R%'")
        if self.cField == 'true':
            self.queryList.append(" class NOT LIKE 'L%'")
        if self.dField == 'true':
            self.queryList.append(" class LIKE 'CI%'")
        if len(self.queryList)>0:
            self.DataQuery = self.queryList[0]
            while i < len(self.queryList):
                self.DataQuery= self.DataQuery + " AND %s" %self.queryList[i]
            self.DataQuery = ""

What is the error in my code or my approach?

  • Can you post the entire code for your class that you're forming? It's hard to debug when we can't see the full code. Have you tried this: '"ATTRIBUTE" = '.format(self.DataQuery) You were also adding a space to the field name ATTRIBUTE. It may also help to add some print statements so you can actually see what the query is.
    – crmackey
    Jan 11, 2016 at 16:42

1 Answer 1


I think you will solve your issue if you specify the location to substitute self.DataQuery in your where statement.

arcpy.SelectLayerByAttribute_management ("lyr", "NEW_SELECTION", '"ATTRIBUTE" = \'{0}\''.format(self.DataQuery))

Also, you can simplify the part where you build the value for self.DataQuery with:

if len(self.queryList) > 0:
    self.DataQuery = ' AND '.join(queryList)
    self.DataQuery = ''

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.