Friday, October 30, 2009

Find and Replace for Geoprocessing

In my last post I explained how to replace values in an attribute table using the Find & Replace... tool that is available under the Options button of the attribute table. This works very nicely for replacing characters in a specified field but unfortunately does not exist as a geoprocessing tool, which means it cannot be used in Model Builder.

I have updated the Batch Edit String Fields toolbox that I blogged about in a previous entry. I have replaced the script formerly called Replace with a script called Find and Replace. This new Find and Replace script searches a field in an attribute table for user defined characters entered in the script GUI, and replaces them with characters, also entered in the script GUI. The script can be drug into Model Builder and be run as a geoprocessing tool.

The new script has been added to the Batch Edit String Fields toolbox (see Batch Edit String Fields - Free Tools for Calculating String Fields for more information on the scripts in this toolbox). You can download the toolbox from the geospatial database. Right click the link and select Save Target As... Unzip the folder and be sure to store the toolbox in the same location as all of the Python scripts – relative paths are used to locate the scripts.

Batch Edit String Fields

Thursday, October 29, 2009

Find and Replace

This post is a response to a question from Ivar that was posted under the Batch Edit String Fields blog entry. The question was:

Brooke, I'm looking for a script that deletes or replaces some characters from string values. I have one field where the string values will be used as names for feature classes when I split the feature class into many, one for each feature. Example: I would like to replace the text "1630089 - 1630" with "16300891630" or "1630089_1630" (I'm not sure which one will work). It is the " - " part of the string that is the problem. I see that I might find a sollution by using several of the scripts posted by you, but if there is one script that will do the job, I'm grateful for directions.


Replacing the text in a string field can be accomplished with the Find and Replace tool available in core ArcGIS Desktop at all license levels. To do this:

1) Start editing.
2) Open the attribute table.
3) Click on the field heading containing the field text you want to replace to ensure only that field will be altered.
4) Click the Options button at the bottom of the attribute table and choose Find and Replace.
4) A dialog box opens with two tabs. Select the Replace tab. Choose to find " - " (without the quotes). If you want to replace it with an under score (like 1630089_1630), enter "_" (without the quotes) in the Replace input box. If you want to combine the text on either side of the " - " (like 16300891630) simply leave the Replace input box blank.

5) Check on the Search Only Selected Field(s) option and select to match Any Part of the text.
6) Select Replace to replace the values one at a time (might be good to do this for the first couple to make sure you are getting the desired result) or select Replace All to replace all of the values at once.
7) Save edits and stop editing.

Thanks for reading my blog and posting your question, Ivar. I hope this does what you are looking to accomplish.