Friday 30 November 2018

python - Arcpy remove invalid character


I've built a model that takes the name of the Excel worksheet containing xy data, and uses that name as the name of the output shapefile. ( Worksheet "massachusetts_sightings" in an xlsx becomes "massachusetts_sightings$.shp"). I do this using Parse Path. Notice that a "$" is added.


If I then feed this shapefile, massachusetts_sightings$.shp into a clip tool (say, to clip out points in the data that are not in my study area), I get an error for an invalid character in the clip tool. My suspicion is the $. I know I could remove this with Calculate Value...but I can't figure out the code block.


My parse path output is called Value:


Codeblock


Can someone help troubleshoot? I haven't used Python in years-


# Import arcpy module
import arcpy


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

# Set Geoprocessing environments
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"

# Script arguments
XY_Table = arcpy.GetParameterAsText(0)


Define_Coordinate_System = arcpy.GetParameterAsText(1)

Projection = arcpy.GetParameterAsText(2)

Geographic_Transformation = arcpy.GetParameterAsText(3)

Study_Area = arcpy.GetParameterAsText(4)

Projected_and_Clipped_Data = arcpy.GetParameterAsText(5)
if Projected_and_Clipped_Data == '#' or not Projected_and_Clipped_Data:

Projected_and_Clipped_Data = "X:\\Tools\\XY\\point_upload\\Data\\%fixed%_Clip.shp" # provide a default value if unspecified

# Local variables:
xy_event = XY_Table
v_defined = xy_event
defined_data = v_defined
v_projected = defined_data
Value = XY_Table

# Process: Parse Path

arcpy.ParsePath_mb(XY_Table, "NAME")

# Process: Make XY Event Layer
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
tempEnvironment1 = gp.workspace
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"
arcpy.MakeXYEventLayer_management(XY_Table, "", "", xy_event, "", "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.workspace = tempEnvironment1


# Process: Copy Features
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
arcpy.CopyFeatures_management(xy_event, v_defined, "", "0", "0", "0")
arcpy.env.scratchWorkspace = tempEnvironment0

# Process: Define Projection
arcpy.DefineProjection_management(v_defined, Define_Coordinate_System)


# Process: Project
tempEnvironment0 = gp.scratchWorkspace
arcpy.env.scratchWorkspace = "X:\\Tools\\XY\\point_upload\\Scratch"
tempEnvironment1 = gp.workspace
arcpy.env.workspace = "X:\\Tools\\XY\\point_upload\\Data"
arcpy.Project_management(defined_data, v_projected, Projection, Geographic_Transformation, "")
arcpy.env.scratchWorkspace = tempEnvironment0
arcpy.env.workspace = tempEnvironment1

# Process: Clip

arcpy.Clip_analysis(v_projected, Study_Area, Projected_and_Clipped_Data, "")

# Process: Calculate Value
arcpy.CalculateValue_management("\"%Value%\".strip(\"$\")", "\\n", "String")


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...