Remove extra spaces from string using the field calculator

16543
12
12-19-2011 07:14 AM
MattGoodman
New Contributor III
Hi, I'm looking for an elegant python expression that I can be used within ArcMap's field calculator to find all instances of consecutive spaces within a string and replace them with a single space.

Example:
[INDENT]Let's say in a field called "Address" we have the following value (the product of poor concatenation):[/INDENT]
[INDENT]"_123_____Main__St,____Townsville,______CA_98463___"[/INDENT]

[INDENT]It should become "123 Main St, Townsville, CA 98463"[/INDENT]


I know that .strip will get rid of the leading/trailling spaces.  How to I get a find/replace approach to continue looping through until all the double-spaces have been reduced to single spaces?  Also, I have tried using .split, but get an error in ArcMap that "Field is not Nullable".

Any help would be appreciated.
Tags (2)
0 Kudos
12 Replies
Luke_Pinner
MVP Regular Contributor
fieldvalue=' '.join(fieldvalue.strip().split())
#in case fieldvalue=='', change empty string to space to avoid "Field is not Nullable" error
if not fieldvalue: fieldvalue=' '
0 Kudos
CraigPrisland
New Contributor
Hello,

I have been using this Python script on one field at a time using the Field Calculator and it is working very well, thank you.  What I am now looking for is to see if there is a Python script that could be run that would look at all fields in a particular feature class and remove any extra spaces in any of those fields.  Any assistance would be greatly appreciated.

Thank you in advance.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Craig,

You could accomplish this with the following code:

fc = "Parcels"

for field in arcpy.ListFields(fc, "*", "String"):
    with arcpy.da.UpdateCursor(fc, field.name) as cursor:
        for row in cursor:            
            value = row[0]
            value = str(value).rstrip()
            row[0] = value
            cursor.updateRow(row)

del cursor
0 Kudos
CraigPrisland
New Contributor
Hi Jake,

Thanks for the quick response and script.  I tried using this script that you provided by replacing "parcels" with the path to my feature class.  I then added this script into ArcToolbox and ran it.  The ran successfully, but when I opened the feature class, the extra spaces did not appear to get removed.  Is there anything else that I am missing?  Thanks again!
0 Kudos
DanPatterson_Retired
MVP Emeritus
Did you reload the fc after the script?  Perhaps you should be working with fc's loaded in as layers rather than running fc from a file on disk
RhettZufelt
MVP Frequent Contributor
Hi Jake,

Thanks for the quick response and script.  I tried using this script that you provided by replacing "parcels" with the path to my feature class.  I then added this script into ArcToolbox and ran it.  The ran successfully, but when I opened the feature class, the extra spaces did not appear to get removed.  Is there anything else that I am missing?  Thanks again!


You said that post 2 worked for you, but only on a single feature.  You can combine the two and use that same coding on the entire FC if you want.  something like this:


arcpy.MakeFeatureLayer_management("C:/data.gdb/parcels", "parcels_lyr")


for field in arcpy.ListFields("parcels_lyr", "*", "String"):

        sqlFieldName = arcpy.AddFieldDelimiters("parcels_lyr", field)         
        calcSql= "' '.join( field.strip().split())"
        arcpy.CalculateField_management("parcels_lyr",field,calcSql,"PYTHON_9.3","#")






This seems to do it for me.

R_

If you wanted to use cursors, post 4 could be used, but use this calc function instead.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Jake,

Thanks for the quick response and script.  I tried using this script that you provided by replacing "parcels" with the path to my feature class.  I then added this script into ArcToolbox and ran it.  The ran successfully, but when I opened the feature class, the extra spaces did not appear to get removed.  Is there anything else that I am missing?  Thanks again!


Are you working with an SDE, File, or Personal Geodatabase?  If you are working with an SDE feature class, you may have to refresh the version using the Refresh tool on the Versioning toolbar.
RhettZufelt
MVP Frequent Contributor
Hi Craig,

You could accomplish this with the following code:

fc = "Parcels"

for field in arcpy.ListFields(fc, "*", "String"):
    with arcpy.da.UpdateCursor(fc, field.name) as cursor:
        for row in cursor:            
            value = row[0]
            value = str(value).rstrip()
            row[0] = value
            cursor.updateRow(row)

del cursor


Am I missing something in this code?  It seems like this would just "strip" the whitespace from the ends and still leave all the internal spaces?

R_
0 Kudos
CraigPrisland
New Contributor
Am I missing something in this code?  It seems like this would just "strip" the whitespace from the ends and still leave all the internal spaces?

R_


Yes, only the whitespace from the ends get stripped with this code and the internal spaces are still present.  I am using this code against file and personal geodatabases.
0 Kudos