Sunday 17 December 2017

arcgis desktop - Renaming a variable input to replace spaces with underscores to be used in an output's name


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!



Answer




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


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...