Using a Value from a list in a Query Clause

266
8
Jump to solution
10-17-2017 08:33 AM
MikeEdwards
New Contributor III

Hello,

I'm trying to export the errors from a Topology Validation. Multiple features are being validated at once and right now the output is three feature classes (poly, line, point) containing the errors from every feature class that has been validated.

I'm trying to change the export to be one for each feature class geometry type combination (stripping out the errors from each individual feature class and exporting them as their own.

What I have right now keeps ending with an Invalid Expression error (ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).).

Can anyone give me a hand figuring out what I'm doing wrong?

import arcpy
from arcpy import env

arcpy.env.overwriteOutput = True


env.workspace = "C:\Temp\Test_Data.gdb"

table = r"C:\Temp\Test_Data.gdb\TOPOLOGY\ArcGIS_Errors_line"
field = "OriginObjectClassName"


with arcpy.da.SearchCursor(table, field) as cursor:
   myValues_List = sorted({row[0] for row in cursor})
   for FeatureClassValue in myValues_List:
      queryClause = '"' + 'OriginObjectClassName = ' + "'" + FeatureClassValue.strip('\n') + "'" + '"'
      arcpy.MakeFeatureLayer_management("TC:\Temp\Test_Data.gdb\TOPOLOGY\ArcGIS_Errors_line","FEATURE_SELECTION")
      arcpy.SelectLayerByAttribute_management("FEATURE_SELECTION", "NEW_SELECTION", queryClause)
      arcpy.CopyFeatures_management("FEATURE_SELECTION", env.workspace + FeatureClassValue + "_Errors")

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Your query contains additional double quotes that shouldn't be there. Try this instead:

queryClause = "OriginObjectClassName = '{0}'".format(FeatureClassValue)

The other thing that I am wondering... is there really an enter after the featureclass name?

View solution in original post

8 Replies
MitchHolley1
MVP Regular Contributor

Have you tried printing the query to verify the syntax?  If so, what does it look like?

0 Kudos
MikeEdwards
New Contributor III

Printing the query gives me:

"OriginObjectClassName = 'BUILDINGS'"
"OriginObjectClassName = 'PARKS'"
"OriginObjectClassName = 'ROADS'"

If I substitute any of those with queryClause it works fine for that one feature.

0 Kudos
MitchHolley1
MVP Regular Contributor

Try putting an 'r' in front of all paths.   Check below

#what you have
arcpy.MakeFeatureLayer_management("TC:\Temp\Test_Data.gdb\TOPOLOGY\ArcGIS_Errors_line","FEATURE_SELECTION")

#what I think it should be
arcpy.MakeFeatureLayer_management(r"C:\Temp\Test_Data.gdb\TOPOLOGY\ArcGIS_Errors_line","FEATURE_SELECTION")‍‍‍
XanderBakker
Esri Esteemed Contributor

Your query contains additional double quotes that shouldn't be there. Try this instead:

queryClause = "OriginObjectClassName = '{0}'".format(FeatureClassValue)

The other thing that I am wondering... is there really an enter after the featureclass name?

MikeEdwards
New Contributor III

Thanks guys, I'll give that a shot.

The TC:\ was a typo, I was just shortening the path names for posting on here.

0 Kudos
XanderBakker
Esri Esteemed Contributor

It may have been a typo, but the comment made by mitchh300  still is valid. Your \T will convert into a TAB and create an invalid path. Please read this blog by Dan_Patterson : /blogs/dan_patterson/2016/08/14/filenames-and-file-paths-in-python 

0 Kudos
MikeEdwards
New Contributor III

That worked!

Thank you very much to both of you.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Great that worked. If it was the suggestion made by mitchh300  that helped you resolve the problem, could you mark his comment as the correct answer?

0 Kudos