<?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: Strip attributes of entire table in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468570#M36574</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;my previous post didn't make much sense.&lt;/P&gt;&lt;P&gt;Current code alters the original and new tables&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to not alter the original table, only alter the exported one?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 04 Jan 2016 19:53:12 GMT</pubDate>
    <dc:creator>2Quiker</dc:creator>
    <dc:date>2016-01-04T19:53:12Z</dc:date>
    <item>
      <title>Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468565#M36569</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a table that is generated else where. the problem i have is that the majority of the field attributes have different amount of spaces before and after the attribute. for example "____Blackhawk Sub No 1 _______". I have been manual using field calculator doing the !myfield!.lstrip, !myfield!.rstrip !myfield!.strip for each field and that sucks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a why to strip all the spaces infron/begining and at the end?&lt;/P&gt;&lt;P&gt;This table has both number and string.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have been trying with the code below but i get an error on line 11. so i am thinking my expression is incorrect?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ERROR 000622: Failed to execute (Calculate Field). Parameters are not valid.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from datetime import datetime as d
startTime = d.now()

arcpy.MakeTableView_management("C:\Temp\ParAdminTable.dbf", "parAdmin")

for field in arcpy.ListFields("parAdmin", "*", "String"):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sqlFieldName = arcpy.AddFieldDelimiters("parAdmin", field)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; calcSql= "' '.join( field.strip().split())"&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("parAdmin",field,calcSql,"PYTHON_9.3","#")&amp;nbsp; 

arcpy.TableToTable_conversion("parAdmin", "C:\Temp" , "ParAdmin_Test.dbf")

try:&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '(Elapsed time: ' + str(d.now() - startTime)[:-3] + ')'

except Exception, e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # If an error occurred, print line number and error message
&amp;nbsp;&amp;nbsp;&amp;nbsp; import traceback, sys
&amp;nbsp;&amp;nbsp;&amp;nbsp; tb = sys.exc_info()[2]
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Line %i" % tb.tb_lineno
&amp;nbsp;&amp;nbsp;&amp;nbsp; print e.message
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468565#M36569</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2021-12-11T20:46:19Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468566#M36570</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I believe you're supposed to pass in the character you want to remove to the strip() function. In your case, field.strip(' '). Note that this won't work on numeric fields, but they won't have blank spaces anyway.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Dec 2015 16:18:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468566#M36570</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2015-12-29T16:18:00Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468567#M36571</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have changed the line &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;calcSql= "' '.join( field.strip().split())"&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;to&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;calcSql= "' '.join( field.strip(' ').split())"&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But i am still getting the same error on the same line.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 29 Dec 2015 16:37:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468567#M36571</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2015-12-29T16:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468568#M36572</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You have two problems. 1) ListFields returns field objects and you're attempting to leverage these as strings when you need to leverage the name property of the field object. 2) your expression is missing the delimiters for the field calculation.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've pasted a corrected copy of your code below along with a couple of alternatives that I thought of off the top of my head.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy


table = r"C:\Temp\ParAdminTable.dbf"


for field in arcpy.ListFields(table, "*", "String"):
&amp;nbsp;&amp;nbsp;&amp;nbsp; calcSql = "' '.join(!{0}!.strip().split())".format(field.name) 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.CalculateField(table, field.name, calcSql, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
arcpy.conversion.TableToTable(table, r"C:\Temp", "ParAdmin_Test.dbf")


# Option 2 : Only removes extra spacing at begining and end
for field in [f.name for f in arcpy.Describe(table).fields if f.type == "String"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; exp = "!{0}!.strip()"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.CalculateField(table, field, exp.format(field), "PYTHON")


# Option 3 : Remove all extra spaces
for field in [f.name for f in arcpy.Describe(table).fields if f.type == "String"]:
&amp;nbsp;&amp;nbsp;&amp;nbsp; exp = "' '.join([t for t in !{0}!.split(' ') if t not in ('', ' ', None)])"
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.management.CalculateField(table, field, exp.format(field), "PYTHON")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 20:46:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468568#M36572</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2021-12-11T20:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468569#M36573</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Code works awesome. only alter the output table?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 17:43:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468569#M36573</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2016-01-04T17:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468570#M36574</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;my previous post didn't make much sense.&lt;/P&gt;&lt;P&gt;Current code alters the original and new tables&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Is there a way to not alter the original table, only alter the exported one?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 19:53:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468570#M36574</guid>
      <dc:creator>2Quiker</dc:creator>
      <dc:date>2016-01-04T19:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468571#M36575</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Export the table first and then execute the calculations against the newly generated table. In your original code this was how you were implementing your business logic.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 20:54:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468571#M36575</guid>
      <dc:creator>FreddieGibson</dc:creator>
      <dc:date>2016-01-04T20:54:41Z</dc:date>
    </item>
    <item>
      <title>Re: Strip attributes of entire table</title>
      <link>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468572#M36576</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;TableToTable&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 04 Jan 2016 21:18:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/strip-attributes-of-entire-table/m-p/468572#M36576</guid>
      <dc:creator>CCWeedcontrol</dc:creator>
      <dc:date>2016-01-04T21:18:49Z</dc:date>
    </item>
  </channel>
</rss>

