Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have some code that is intended to remove spaces from some data I'm ingesting from a group of shapefiles into a number of feature classes. The malformed data consistently looks like this:

ABC12345678 90

and I want to make it look like:


Simple, right? So the code block goes:

with arcpy.da.UpdateCursor(fc, 'FIELD') as cursor:
    for rows in cursor:
        if rows:
            firstValue = rows
            secondValue = firstValue.replace(' ', '')
            rows = secondValue[:13] #this is because some rows have more than 13 characters

I've inserted print statements and it is getting into the loop, but the .replace method is not doing anything.

I've also tried swapping out .replace() with

secondValue = (firstValue[:11] + firstValue[12:])

and no dice there either.

The print statements show:

[u'ABC12345678 90']

through the entire loop - neither replace() nor concatenation is having any effect.

Is it possible that this is not actually a space character? Could this be a tab value or something which is causing this code to fail?

share|improve this question
secondValue = (firstValue[:11] + firstValue[12:]) should work although I'd take it out of the parentheses. Better would be to use secondValue = '{}{}'.format(firstValue[:11], firstValue[12:]) – Midavalo Mar 28 at 20:19
thanks - the '{}{}'.format() is definitely more pythony - but alas, it does not work. still prints out the same thing. – auslander Mar 28 at 20:29
up vote 3 down vote accepted

Is that the actual code you are using? You should be getting an error in this line:

secondValue = firstValue.replace(' ','')

You should be getting an AttributeError: 'tuple' object has no attribute 'replace' because your rows variable is a tuple returned by the Cursor, which represents a tuple of your row values. So in the example you have, the rows will be:

('ABC12345678 90',) # this is a tuple

Are you just trying to remove the whitespace in the value? If so, why not just use:

with arcpy.da.UpdateCursor(fc, 'FIELD') as cursor:
    for row in cursor:
        row[0] = row[0].replace(' ','')
share|improve this answer
Aha, that did it, but it needs to be cursor.updateRow(row). Thanks! BTW, It wasn't giving me an attribute error because I have it within a try: loop. – auslander Mar 28 at 20:37
That makes sense. The problem was you were referencing the whole row object, which is either a list or tuple that gets returned from the cursor. To set individual field values, you need to reference that value by the index that matches the field. – crmackey Mar 28 at 20:40
cursor.updateRow(row) fixed – Midavalo Mar 29 at 4:36

There are an assortment of unicode characters that look like spaces, but aren't, such as "\xa0" which is a non-breaking space. To find these, I open my data in an advanced text editor like Notepad++, and encode the data in UTF-8. If I see non-ASCII characters, I know to remove them.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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