0
votes

I have the following string which forces my Python script to quit:

"625             625 QUAIL DR UNIT B"

I need to delete the extra spaces in the middle of the string so I am trying to use the following split join script:

import arcgisscripting
import logging 
logger = logging.getLogger()

gp = arcgisscripting.create(9.3)
gp.OverWriteOutput = True

gp.Workspace = "C:\ZP4"
fcs = gp.ListWorkspaces("*","Folder")

for fc in fcs:

    print fc
    rows = gp.UpdateCursor(fc + "//Parcels.shp")
    row = rows.Next()
    while row:
        Name = row.GetValue('SIT_FULL_S').join(s.split())
        print Name
        row.SetValue('SIT_FULL_S', Name)
        rows.updateRow(row)
        row = rows.Next()
    del row
    del rows
4
Have you saved your file recently and are you sure you're working with the same file? The code posted doesn't seem to correspond to the error; Line 17 doesn't match the line reproduced in the error. - Hoa Long Tam

4 Answers

4
votes

Your source code and your error do not match, the error states you didn't define the variable SIT_FULL_S.

I am guessing that what you want is:

Name = ' '.join(row.GetValue('SIT_FULL_S').split())
3
votes

Use the re module...

>>> import re
>>> str = 'A   B C'
>>> re.sub(r'\s+', ' ', str)
'A B C'
2
votes

I believe you should use regular expressions to match all the places where you find two or more spaces and then replace it (each occurence) with a single space.

This can be made using shorter portion of code:

re.sub(r'\s{2,}', ' ', your_string)
0
votes

It's a bit unclear, but I think what you need is:

" ".join(row.GetValue('SIT_FULL_S').split())