-1

If I have a field say 'account' and I want all the white space (if there is any to the right and left of the account entry, for example 83128313 - is there a python script or field calculator function that can do this for me?

7

You are looking for the str.strip() method:

Calculate your field to be:

!account!.strip()

This will remove all the whitespaces both to the left and to the right.

Having this:

s = ' 83128313 '

would result after:

s.strip()

in

'83128313'

  • Hi Alex, thanks for this I am running this now, on 80000 records it seems to take along time, should this be instant, or will it 'churn' alittle? – Pob 1 Aug 17 '16 at 16:47
  • 2
    it depends a bit on the hard disk you have. On an average professional laptop, running this on the 80,000 rows wouldn't take more than 10-20 secs top. On mine, with SSD disk, it took just about 5 secs. I believe running this in the editing session would take longer time, too. – Alex Tereshenkov Aug 17 '16 at 17:17
-1

With ArcPy you would do the following:

import arcpy

with arcpy.da.UpdateCursor(r'pathToYourDataset',['theField']) as cursor:
    for row in cursor:
        row[0] = row[0].strip()
        cursor.updateRow(row)

All you have to do is replace theField with the name of the field you want to update, and pathToYourDataset with the path to your dataset, such as:

with arcpy.da.UpdateCursor(r"D:\PythonTesting\randomData\City.shp",['spatialID']) as cursor:
  • 2
    please fix the syntax error - the cursor takes the fc first and then the fields. – Alex Tereshenkov Aug 17 '16 at 17:26
  • 1
    This won't work - your updatecursor is set up wrong – Midavalo Aug 17 '16 at 18:24
  • True, I wrote it from memory and got the parameters mixed up. You could have edited it yourself to provide faster help :-) – BritishSteel Aug 18 '16 at 9:42

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