我有以下字符串强制我的Python脚本退出:
"625 625 QUAIL DR UNIT B"
我需要删除字符串中间的额外空格,所以我尝试使用以下拆分连接脚本:
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
答案 0 :(得分:4)
您的源代码和错误不匹配,错误表明您没有定义变量SIT_FULL_S
。
我猜你想要的是:
Name = ' '.join(row.GetValue('SIT_FULL_S').split())
答案 1 :(得分:3)
使用re模块...
>>> import re
>>> str = 'A B C'
>>> re.sub(r'\s+', ' ', str)
'A B C'
答案 2 :(得分:2)
我相信你应该使用正则表达式来匹配你找到两个或更多空格的所有地方,然后用一个空格替换它(每个出现)。
这可以使用较短的代码部分进行:
re.sub(r'\s{2,}', ' ', your_string)
答案 3 :(得分:0)
有点不清楚,但我认为你需要的是:
" ".join(row.GetValue('SIT_FULL_S').split())