Hello i have a pnt shp with 50 pnts. I want to export each one of it in a separate shapefile with arcpy writting a loop. My problem is that i want the output name i create to have a different name, e.g. for 1st pnt i want the output layers' name to be pnt1_lyr, for the 2nd point, pnt2_lyr etc. I wrote that code but i always have an error. Can someone correct me;
import arcpy
arcpy.env.workspace = "c:/Wing/Sk"
for i in range(0, 49):
arcpy.MakeFeatureLayer_management("spatially50.shp", "pnt_" + i + "lyr", "FID = i")
Thank you very much
Solved! Go to Solution.
actually FID is a numeric field, so a str(i) there would crash the query clause, since its comparing a number to a string.
I made some sample data and tested the following that works.
import arcpy arcpy.env.workspace = "c:/Wing/Sk" for i in range(0, 50): where_clause = "FID = {}".format(i) arcpy.FeatureClassToFeatureClass_conversion("spatially50", "c:/Wing/Sk", "pnt" + str(i), where_clause)
Formatting queries in python can be annoying, string formatting makes this much easier.
I usually refer back to this thread for properly creating SQL queries Python - Select by attributes query issue
actually FID is a numeric field, so a str(i) there would crash the query clause, since its comparing a number to a string.
No. " FID = " + str(i) results in a whereclause of " FID = 0 " (desired) not " FID = '0' " (what you predict). Your method may work, but so does mine.
Sorry you were completely right about that, and to boot its faster since you aren't setting a variable each time.
i was missing that: "FID=" + str(i). For God that was really helpfull, thank you for your try Mr. Wiens.
If you are only working with Points, another approach is to use the new NumPy-based features that were introduced with the ArcPy data access (arcpy.da) module.
import os import numpy as np in_features = #Point data set to have exported as individual data sets out_path = #Path to geodatabases or folder for individual data sets out_prefix = 'pnt_' #Prefix for individual data sets, can be empty '' desc = arcpy.Describe(in_features) SR = desc.spatialReference OID_name = desc.OIDFieldName shape_name = desc.ShapeFieldName narr = arcpy.da.FeatureClassToNumPyArray(in_features, "*") ndtype = narr.dtype for row in narr: n = np.array(row, ndtype) OID = n[OID_name] arcpy.da.NumPyArrayToFeatureClass(n, os.path.join(out_path, out_prefix + str(OID)), shape_name, SR)
The above script will work on shape files and feature classes because I am using an arcpy.Describe object to retrieve the name of the unique identifier and shape fields.
Thank you a lot Mr. Bixby