Select to view content in your preferred language

Script Find and Replace

862
4
Jump to solution
06-24-2013 10:22 AM
JoshSaad1
Frequent Contributor
After I export my data to a shapefile, I have a range of addresses for which I need to replace their suffixes from Dr to Ave.  I'm using a process I developed in Model Builder to perform the export.  Is there a way to script the edit, and integrate it into my process model?  Thanks.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
After I export my data to a shapefile, I have a range of addresses for which I need to replace their suffixes from Dr to Ave.  I'm using a process I developed in Model Builder to perform the export.  Is there a way to script the edit, and integrate it into my process model?  Thanks.


Use the Make Feature Layer tool on the export output.  Then use the Select by Attributes tool to select the address range.  Then use the Calculator Field tool.  Basically the same as what you would most likely do in ArcMap.  See the Data Management toolbox in the Layers and Table Views toolset for the first two toola and the Fields toolset for the Calculate Field tool.

View solution in original post

0 Kudos
4 Replies
RichardFairhurst
MVP Honored Contributor
After I export my data to a shapefile, I have a range of addresses for which I need to replace their suffixes from Dr to Ave.  I'm using a process I developed in Model Builder to perform the export.  Is there a way to script the edit, and integrate it into my process model?  Thanks.


Use the Make Feature Layer tool on the export output.  Then use the Select by Attributes tool to select the address range.  Then use the Calculator Field tool.  Basically the same as what you would most likely do in ArcMap.  See the Data Management toolbox in the Layers and Table Views toolset for the first two toola and the Fields toolset for the Calculate Field tool.
0 Kudos
YasarKorkmaz
Occasional Contributor
This worked for me to clean up a specific field (PARCELNO) in multiple layers in a geodatabase. Specifically, the script deletes the "-" and " " in the PARCELNO field and replaces with nothing so that I get a PARCELNO field without spaces and "-". Since I had other fields in the layers I had to specify the field name to clear "-" and " " from.

import arcpy
import os
import string
import re
arcpy.env.workspace = r'C:\Workspace\PARCELIDCLEANUP.gdb'
fcs = arcpy.ListFeatureClasses()
# Create list of fields for keeping
 
s = ['PARCELNO']
for fc in fcs:
    print "** Now working on "+fc
    fields = arcpy.ListFields(fc)
    for field in fields:
        if field.baseName in s:
            rows = arcpy.UpdateCursor(fc)
            for row in rows:
                targetRow = row.PARCELNO
                row.PARCELNO = targetRow.replace(' ','')
                row.PARCELNO = targetRow.replace('-','')
                rows.updateRow(row)
            del row
            del rows
            print "   Parcelno cleaned"
        if not field.baseName in s:
            None
print "PARCELNO is edited"
0 Kudos
JoshSaad1
Frequent Contributor
Thanks for your help.  I was able to build the process in Model Builder and integrate it with my data export model.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Thanks for your help.  I was able to build the process in Model Builder and integrate it with my data export model.


Please mark the post that answered your question to close this thread and award points if a post was helpful. See the tag line of my post for guidance.
0 Kudos