Using ArcGIS 10.4.1 and Arcpy I am trying to select a shapefile by it attributes and create new feature class based on unique values. I am able to create new fs inside the Mapset.gdb/Linear but it is not producing based on "NEW_SELECTION". In Fact I am copying entire Roads.shp in each of feature classes.

How can I fix this?

import arcpy
from arcpy import env

def unique_values(table , field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})

uniques = unique_values(r'D:/GIS/Roads.shp' , 'fclass')  

for unique in uniques:
    arcpy.MakeFeatureLayer_management(r'D:/GIS/Roads.shp', unique)
    arcpy.SelectLayerByAttribute_management (unique, "NEW_SELECTION", " 'fclass' = '"+unique+"' ")
    arcpy.CopyFeatures_management(unique, r"D:/GIS/Mapset.gdb/Linear/"+unique+"")
  • 1
    Have you tried performing this manually and then checking the python snippet? I find this helps me spot hiccups in my code. Nov 28 '17 at 18:44
  • 1
    Where is uniques created?
    – Midavalo
    Nov 28 '17 at 18:50
  • 1
    @MonaCoder No, I mean it is not created in your code. There is no uniques = [some list of values] in your code. Have you actually run the code you've pasted in your question? Or have you pasted something different to what you've tested?
    – Midavalo
    Nov 28 '17 at 18:54
  • 1
    Put a print uniques immediately after your uniques = unique_values(r'D:/GIS/Roads.shp' , 'fclass') line and see if it prints anything
    – Midavalo
    Nov 28 '17 at 19:00
  • 1
    can you try changing this " 'fclass' = '"+unique+"' " to this " fclass = '"+unique+"' "
    – gotchula
    Nov 28 '17 at 23:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.