开发者

Remove extra spaces in middle of string split join Python

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


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())


Use the re module...

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


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)


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜