Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

This may be easier than i am making it. Using Model Builder, I have a variable input "Name", which is set as a model parameter, used for a 'Select by Attribute' early in the model. I would like to then use this variable as an input into the final feature class output's name. My issue is that some of these "Names" have spaces in them (E.G. Name = "Watershed 1"). Is there a way, using Model Builder, that i can replace the spaces with underscores/remove the spaces before inserting the variable into the output's name?

Resulting in (....spatialjoin_dissolve_Watershed_1)

Since this model will be run individually for multiple areas i need the name of the watershed to be included, without having to have the user enter in the watershed name with underscores as a parameter.

Thanks for any help!

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

You can do this using the Calculate Value tool to modify the "Name" string variable to replace spaces with underscores. When you are saving the Spatial Join feature class output, you can then use inline variable substitution:

C:\gisworkspace\WORKSPACE\testing_workspace\%output_value%.shp

....where 'output_value' is the output name from the Calculate value tool.

Here is what the parameters of the Calculate Value could look like:

Expression:

ReplaceName('%StringInput%')

....where 'StringInput' is the name of your 'Name' model parameter

Code Block:

def ReplaceName(name) :
  return name.replace(" ", "_")

Data type:

String

Here is a quick example in ModelBuilder:

enter image description here

Full Size Image

share|improve this answer
1  
This fits very easily into my model. Very Helpful, Thanks. –  JB_Env_Science_BGE Apr 9 at 20:20
add comment

The easiest way to do this would be to use the arcpy.ValidateTableName(fc, out_workspace) function, which will automatically convert any invalid characters (like spaces) into valid characters (underscores).

share|improve this answer
    
Esri Forum post for using this function in the Calculate Value tool in Modelbuilder. –  amarinel Apr 8 at 17:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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