I'm new to Python and I need a script that can split my huge shapefile based on a certain attribute.
SO what I have is a big shapefile (over 3M records) with a field named "NISCODE".
In this code are values ranging from 11001 to 73109. But only the first number of the value is important to me, that's the base on which the shapefile needs to be split on.
So I need 7 shapefiles; everything starting with 1, with 2,...
I know I'll probably need a for loop, but I don't know how to put it in my code correctly. Here is what I have so far, this was just a test to see if I could already make a selection for the 1st part (everything starting with 1...), but already that fails.
# Import system modules
import arcpy
from arcpy import env
env.overwriteOutput="True"
# Set workspace
# setting the environment
workspacedir= r"C:\..."
env.workspace= workspacedir
shp = r"C:\....\Adp.shp"
r=arcpy.Exists("Adp.shp")
print r
### Make a layer from the feature class
arcpy.MakeFeatureLayer_management("Adp.shp", "Vl_lyr")
##
# Wselect only those that have id starting with 1
arcpy.SelectLayerByAttribute_management("Vl_lyr", "NEW_SELECTION", ' "NISCODE" = 1* ')
##
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("Vl_lyr", "Vlaanderen_1.shp")
EDIT:
The new code looks now like this:`
# Import system modules
import arcpy
from arcpy import env
env.overwriteOutput="True"
# Set workspace
# setting the environment
workspacedir= r"C:\..."
env.workspace= workspacedir
shp = r"C:\...\Adp.shp"
arcpy.AddField_management(shp, "newNISCODE", "TEXT")
arcpy.CalculateField_management(shp, "newNISCODE", '"newNISCODE" = NISCODE[0]', "PYTHON")
list = []
with arcpy.da.SearchCursor(fc, "newNICODE") as cursor:
for row in cursor:
list.append(row)
num_list = sorted(set(list))
for i in range(1, len(num_list)):
arcpy.Select_analysis(shp, "{}_shp".format(i), 'newNICODE = "{}"'.format(i))`
but the code fails on the Calculatefield step:
Traceback (most recent call last):
File "C:\...\SplitShape.py", line 15, in <module>
arcpy.CalculateField_management(shp, "newNISCODE", '"newNISCODE" = NISCODE[0]', "PYTHON")
File "C:\Program Files (x86)\ArcGIS\Desktop10.5\ArcPy\arcpy\management.py", line 3663, in CalculateField
raise e
ExecuteError: ERROR 000539: SyntaxError: invalid syntax (<expression>, line 1)
Failed to execute (CalculateField).