<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: replace double space with single spaces in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595239#M73909</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;fc = "layer"  # path to layer
field_name = "Field"  #  field name

arcpy.management.CalculateField(fc, field_name, "!{}!.replace('  ', ' ')".format(field_name), "PYTHON3")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you wan to do multiple spaces with a single space in addresses you can do something like this,&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy

# Replace multiple spaces with a single space in a string
def replace_multiple_spaces(address):
    return ' '.join(address.split())

# Example: Update a feature class field with cleaned addresses
def clean_addresses_in_feature_class(feature_class, address_field):
    with arcpy.da.UpdateCursor(feature_class, [address_field]) as cursor:
        for row in cursor:
            address = row[0]
            if address:  # Check if the address is not empty
                cleaned_address = replace_multiple_spaces(address)
                row[0] = cleaned_address
                cursor.updateRow(row)

# Example usage
feature_class = "layer"  # layer
address_field = "Field"  # Field  addresses
clean_addresses_in_feature_class(feature_class, address_field)

print("Addresses cleaned successfully!")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Mar 2025 16:21:14 GMT</pubDate>
    <dc:creator>TonyAlmeida</dc:creator>
    <dc:date>2025-03-13T16:21:14Z</dc:date>
    <item>
      <title>replace double space with single spaces</title>
      <link>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595222#M73907</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am trying to replace double spaces in an address with a single space. In pro, I can do this using .replace() like this !Service_Address!.replace("&amp;nbsp; "," ")&lt;/P&gt;&lt;P&gt;but when I try this within arcpy, I get an error&lt;/P&gt;&lt;P&gt;arcgisscripting.ExecuteError: ERROR 000539: File "&amp;lt;expression&amp;gt;", line 1&lt;BR /&gt;&amp;nbsp;u"106 NE ENTRADA AVE".replace( , )&lt;/P&gt;&lt;P&gt;The replace parameters are not showing.&lt;/P&gt;&lt;P&gt;any ideas?&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 15:37:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595222#M73907</guid>
      <dc:creator>MichaelKohler</dc:creator>
      <dc:date>2025-03-13T15:37:24Z</dc:date>
    </item>
    <item>
      <title>Re: replace double space with single spaces</title>
      <link>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595229#M73908</link>
      <description>&lt;P&gt;this worked&lt;/P&gt;&lt;LI-CODE lang="python"&gt;calc = "!matched!.replace('  ',' ')"
arcpy.management.CalculateField(cis_tbl,"matched",calc,'PYTHON3')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 15:49:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595229#M73908</guid>
      <dc:creator>MichaelKohler</dc:creator>
      <dc:date>2025-03-13T15:49:58Z</dc:date>
    </item>
    <item>
      <title>Re: replace double space with single spaces</title>
      <link>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595239#M73909</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;fc = "layer"  # path to layer
field_name = "Field"  #  field name

arcpy.management.CalculateField(fc, field_name, "!{}!.replace('  ', ' ')".format(field_name), "PYTHON3")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you wan to do multiple spaces with a single space in addresses you can do something like this,&lt;/P&gt;&lt;LI-CODE lang="c"&gt;import arcpy

# Replace multiple spaces with a single space in a string
def replace_multiple_spaces(address):
    return ' '.join(address.split())

# Example: Update a feature class field with cleaned addresses
def clean_addresses_in_feature_class(feature_class, address_field):
    with arcpy.da.UpdateCursor(feature_class, [address_field]) as cursor:
        for row in cursor:
            address = row[0]
            if address:  # Check if the address is not empty
                cleaned_address = replace_multiple_spaces(address)
                row[0] = cleaned_address
                cursor.updateRow(row)

# Example usage
feature_class = "layer"  # layer
address_field = "Field"  # Field  addresses
clean_addresses_in_feature_class(feature_class, address_field)

print("Addresses cleaned successfully!")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 16:21:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/replace-double-space-with-single-spaces/m-p/1595239#M73909</guid>
      <dc:creator>TonyAlmeida</dc:creator>
      <dc:date>2025-03-13T16:21:14Z</dc:date>
    </item>
  </channel>
</rss>

