Select to view content in your preferred language

How to remove a leading space

4976
7
05-12-2014 01:15 AM
ThomasTillmann
Emerging Contributor
Hello,

in some lines in a field I have a leading space before the letters. I need to remove this space.
Is there a simple field calculation code to remove the space, or simpler, to remove the first character (whatever it is)?

Thanks,
Tom
Tags (2)
0 Kudos
7 Replies
AhmedEl-Sisi
Deactivated User
Use str.lstrip([chars]) in Python Expression.
you can pass chars parameter as you like.
In case of  leading space  it will be !YourField!.lstrip(' ')

Something like this:
import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.AddField_management("vegtable.dbf", "VEG_TYP2", "TEXT", "", "", "20")
arcpy.CalculateField_management("vegtable.dbf", "VEG_TYP2", 
                                '!Name!.lstrip(' ')', "PYTHON")


More Info:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000004m000000

Regards,
0 Kudos
ThomasTillmann
Emerging Contributor
Thanks for the quick reply! I tried to get it done but got an error.

The field with the leading space would be "data1", I added a second field "data2" in which I want to field calculate the entries from data1 without the leading space. So I tried this:

import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.AddField_management("shapefile_name.dbf", "data1", "TEXT", "", "", "20")
arcpy.CalculateField_management("shapefile_name.dbf", "data2",
                                '!data1!.lstrip(' ')', "PYTHON")

I apply the field calculation in data2, using the box below "data2=", without having checked the box "advanced" (no VBA Script code). This is in ArcGIS 9.3., the table of a polygon shape file.
0 Kudos
Zeke
by
Honored Contributor
Where are you performing this calculation? Script, python window, field calculator? Also, there's nothing to calculate for data2. You're adding the field data1, but there's no data in it at that point. There's nothing to run lstrip() on. You need to populate it.

edit: you also want to use "PYTHON_9.3" for your setup, not just "PYTHON"
0 Kudos
ThomasTillmann
Emerging Contributor
Ok, I populated data2 with the data from data1. Then I tried this code:

import arcpy
from arcpy import env
env.workspace = "C:/data"
arcpy.AddField_management("shapefile_name.dbf", "data2", "TEXT", "", "", "100")
arcpy.CalculateField_management("shapefile_name.dbf", "data2",
                                '!data2!.lstrip(' ')', "PYTHON_9.3")


Still I get the error "user iterrupt".

I perform the calcuation in the field calculator window from the attribute table of the shapefile.
0 Kudos
Zeke
by
Honored Contributor
No, don't use this in Field Calculator. Open the Python window from the toolbar and enter it there. Check your CalculateField call though. It appears you're trying to calculate data2 on itself, but there's nothing in data2.  You probably want to change data2.lstrip(' ') to data1.lstrip(' '). You're also not enclosing both datax fields in !'s. Also, since you already created data2, no need, and probably throw an  error, to add it again. If you have data2 already, delete the AddField  line.

edit - I see you populated data2 first, but there's no need to do this.
edit * 2 - since you already have field data2, you can just use regular Field Calculator. If the leading character in your values for data2 is the only space in them, you can use Replace(' ', '') with the default VB parser (all characters in Replace() are single quotes).

If you have other spaces in the values, switch to Python parser, add data2 (or data1, won't matter), and add [1:]. So !data2![1:]. Or you can use lstrip as above - !data2!.lstrip(). Lstrip will default to using whitespace, so no need to add ' ' to it.
0 Kudos
StevenGraf1
Frequent Contributor
I know this is a python forum, but the VBA script in the field calculator is LTrim([field]) or RTrim([field]).  This will remove leading and trailing spaces.  If there are no other spaces in your data you could do a find and replace.  Find the space, replace it with nothing.  CTRL + F to open the find and replace window in the attribute table.

Hope this helps.
0 Kudos
JoshuaChisholm
Frequent Contributor
I'm not sure if this is part of the problem, but you can't have single quotes'' in other single quotes'' or python gets confused.
#Change this:
arcpy.CalculateField_management("shapefile_name.dbf", "data2", '!data1!.lstrip(' ')', "PYTHON_9.3")
#To this:
arcpy.CalculateField_management("shapefile_name.dbf", "data2", "!data1!.lstrip(' ')", "PYTHON_9.3")

Also (as Greg said), if you just use .lsrtip() the defualt is it remove any whitespace. Like this:
arcpy.CalculateField_management("shapefile_name.dbf", "data2", "!data1!.lstrip()", "PYTHON_9.3")


Hope it helps!
0 Kudos