Possible Duplicate:
Arcpy remove invalid character

I've created a model that iterates through a shape file and pulls out unique values in a field. These unique values are then used to create new shapefiles with the name of the shapefile being the unique value. The problem is I have illegal characters (such as "-") in the unique values. How can I strip these out before the shapefile is made?

Here's the python:

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Script arguments
Inc_Birds2_shp = arcpy.GetParameterAsText(0)
if Inc_Birds2_shp == '#' or not Inc_Birds2_shp:
Inc_Birds2_shp = "C:\\Users\\OJB\\Desktop\\BurdsGIS\\Peru\\GIS\\Created\\Inc_Birds2.shp" # provide a default value if unspecified

# Local variables:
v_Output_Values_ = Inc_Birds2_shp
v_Val__shp = v_Output_Values_
Val = Inc_Birds2_shp
Testing = "C:\\Users\\OJB\\Desktop\\BurdsGIS\\Peru\\GIS\\Created\\Testing"

# Process: Iterate Feature Selection
arcpy.IterateFeatureSelection_mb(Inc_Birds2_shp, "Com_Name #", "false")

# Process: Feature Class to Feature Class
arcpy.FeatureClassToFeatureClass_conversion(v_Output_Values_, Testing,

I'm new to python and prefer working in Model Builder, but I think there is a strip function somewhere. I'm guessing I need to strip the illegal characters out of v_Output_Values_

Any help appreciated

marked as duplicate by blah238, whuber Oct 13 '12 at 15:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Might also want to look into this tool by Dan Patterson: Split Layer By Attributes – blah238 Oct 13 '12 at 5:42
  • Thanks blah, the rewritten script for 10 works well. I could only find the 9.3 version yesterday and was having difficulty rewriting it for 10. – Oliver Burdekin Oct 13 '12 at 12:14

Python strip doesn't quite work like that - it only strips from the beginning and/or end of the string. Perhaps the replace method will work for you. Here's an example.

>>> str01 = "alpha-beta"
>>> str02 = str01.replace('-','_')
>>> str02

(The >>> are meant to represent prompts in your interpreter, if that wasn't clear.)

  • You could also put the Python code into a Calculate Field tool if you want to perform the operation in ModelBuilder. – PolyGeo Oct 13 '12 at 3:53
  • Calculate Value* – blah238 Oct 13 '12 at 5:43

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