Select to view content in your preferred language

Geoprocessing Error Python

1663
4
Jump to solution
09-14-2022 02:20 PM
JacobSpear1
Occasional Contributor

I am working on a project for school and I am trying to create new layers from an existing shp based on attributes. The problem I am having now is that the select by attributes is not actually selecting the appropriate items and exports a layer with the same amount of points as the first layer. I have double and triple checked I have the correct spelling of the data field and attributes. I have also used numerous other methods of creating the expression which return an invalid expression error.

The first screenshot is the error I get with an improper syntax for the SQL query. The second screenshot is the result of the code with a "working" syntax but it creates a new layer with a new name but is otherwise the exact same. Finally, the third screenshot is the csv used as the input.

 

 

Tags (2)
0 Kudos
2 Solutions

Accepted Solutions
RhettZufelt
MVP Notable Contributor

Instead of the select by attribute, try using a make feature layer with where clause, then copy the feature layer to a new fc.

Might also try adding something like int(arcpy.GetCount_management(out_lyr).getOutput(0)) right after the select by atts and see how many are actually being selected.

R_

View solution in original post

0 Kudos
JacobSpear1
Occasional Contributor

This worked! I completely ignored the select line of code and used feature class to feature class with the above suggestion for the SQL expression and got the intended result!

Below is the line of code that worked. I simply changed the variables for each item as necessary

arcpy.conversion.FeatureClassToFeatureClass(out_lyr, "", lived, "DES = 'lived'")

View solution in original post

0 Kudos
4 Replies
graeme_hill
Occasional Contributor

Try running the geoprocessing tool select by attribute manually in arcmap/pro when correct and it's selecting the appropriate records you can copy the python code from the geoprocessing history. Basically the syntax for your where expression isn't right but running the tool manually will help you create it correctly.

Cheers, Graeme
0 Kudos
JacobSpear1
Occasional Contributor

I just tried that and received the same result. The selection query had an underscore after DESC which I assume is just a typo on my part, but it ran with no issues but still created a layer with the same amount of points.

 

0 Kudos
RhettZufelt
MVP Notable Contributor

Instead of the select by attribute, try using a make feature layer with where clause, then copy the feature layer to a new fc.

Might also try adding something like int(arcpy.GetCount_management(out_lyr).getOutput(0)) right after the select by atts and see how many are actually being selected.

R_

0 Kudos
JacobSpear1
Occasional Contributor

This worked! I completely ignored the select line of code and used feature class to feature class with the above suggestion for the SQL expression and got the intended result!

Below is the line of code that worked. I simply changed the variables for each item as necessary

arcpy.conversion.FeatureClassToFeatureClass(out_lyr, "", lived, "DES = 'lived'")

0 Kudos