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.
Solved! Go to Solution.
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_
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'")
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.
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.
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_
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'")