0

I have this code:

import arcpy
import os

def massiveTrim (vrb):
    y=r"shape path"
    for i in y:
        campos = arcpy.ListFields(vrb, "", "String" )
        for camp in campos:
            espacio = "Trim( ["+str(camp.name)+"] )"
            arcpy.CalculateField_management(vrb, str(camp.name), espacio, "VB", "")

print "Finish process"

The script use the TRIM function for delete before and after spaces in a text string for all fields in a shape or FC, this task is automatic, the problem is that TRIM doesn't work. I have ArcGIS 10.5, Pyscripter

5
  • You've misrepresented the problem here. It's not ArcPy that isn't honoring a trim, it's the scripting engine of the (unspecified) database target of CalculateField. Since you've hidden what the vrb variable represents, you now need to provide it in the question.
    – Vince
    May 28 '20 at 23:55
  • 2
    No such function in Python?
    – FelixIP
    May 29 '20 at 0:33
  • Use listfields, da.UpdateCursor and strip. Not calculate field
    – BERA
    May 29 '20 at 8:19
  • hi, if I delete function def massiveTrim (vbr) the script calculate the fields of the feature and TRIM, but very slowly, with VB in line arcpy.CalculateField_management(vrb, str(camp.name), espacio, "VB", "") it define visual basic parser. May 29 '20 at 22:18
  • @FelixIP it define in expression_type (parser) with VB, in function arcpy.CalculateField_management May 29 '20 at 22:27
1

Use listfields, da.UpdateCursor and strip.

import arcpy
y=r"shape path"
fieldlist= [f.name for f in arcpy.ListFields(y, "", "String" )]
with arcpy.da.UpdateCursor(y,fieldlist) as cursor:
    for row in cursor:
        row = [val.strip() for val in row]
        cursor.updateRow(row)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.