2

I wish to loop function that clips a shapefile by a feature class. I want to make the clip the same as the input name. The names of the files are WVHGT_201403281200_f180_201404050000.shp and I just want f180 part as the output feature class. The clip feature will remain consistent throughout.

I have the syntax down from here. I am just not sure how the write the for loop to do this.

  • The name part, is it always between the second and third _ character? – artwork21 Apr 4 '14 at 19:28
  • are you looping through a number of individual feature classes or polygons in a single feature class? – Wes Apr 4 '14 at 19:31
  • Single featureclasses. Once the clip is complete they will all be combined into one geodatabase. – user28563 Apr 7 '14 at 15:51
  • Yes, the name is static and is always between the second and third _. – user28563 Apr 7 '14 at 15:51
4

You can use Python's built-in .split() to extract that part of the name:

file = "WVHGT_201403281200_f180_201404050000.shp"
name = file.split("_")
print name[2]

>>>
f180

Now, integrate this into the for loop. I am assuming you are iterating through a collection of files rather than individual features.

import arcpy, os
from arcpy import env

env.workspace = r"C:\temp1"
outws = r"C:\temp2"

clip_features = r"C:\temp\clipper.shp"

fcs = arcpy.ListFeatureClasses()

for fc in fcs:
    name = fc.split("_")[2] # Extract part of the name
    out_feature_class = os.path.join(outws, name)  # Combine out workspace and filename 
    # Execute Clip
    arcpy.Clip_analysis(fc, clip_features, out_feature_class)
  • Need to add the .shp extension to name variable if the output data type is shapefile. – artwork21 Feb 3 '16 at 13:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.