def trim(fieldvalue): fieldvalue=' '.join(fieldvalue.strip().split()) # in case fieldvalue=='', change empty string to space # to avoid "Field is not Nullable" error if not fieldvalue: fieldvalue=' ' return fieldvalue
fieldvalue=' '.join(fieldvalue.strip().split()) #in case fieldvalue=='', change empty string to space to avoid "Field is not Nullable" error if not fieldvalue: fieldvalue=' '
Output = [MyField] ' Replace MyField with the actual text field name If Not IsNull(Output) then Output = Trim(Output) If Output > "" Then Do While InStr(1, Output, " ") Output = Replace(Output, " ", " ") Loop Else Output = " " ' Avoid Field Is Not Nullable Error End If Else Output = " " ' Field allowed Nulls, use of this option replaces them with white space. End If
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_
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
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
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!
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","#")
Membros conectados podem postar, seguir atualizações e mais. Novo aqui? Registre uma conta gratuita.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.