Python分隔字符串

  在用ArcMap制图时,某些标注过长,地图显示上不太好看,而Maplex扩展可以将标注分行显示,但是首先要将标注字符串用符号分隔。下面的代码是用Python根据一定的规则分隔字符串。这个规则最好的情况下是要有个语义字典库,这样才能保证字符串分割比较符合语言习惯。 #coding=gbk import sys, string, os, arcgisscripting gp = arcgisscripting.create() def subString(str, fromindex, toindex): return str.decode('gbk')[fromindex:toindex].encode('gbk') #pfeatureclass = "D:\\Data\\ChinaAdmin\\entern.shp" #srcfield = "shanghai_4" #destfield = "Form_Name" pfeatureclass = gp.getparameterastext(0) srcfield = gp.getparameterastext(1) destfield = gp.getparameterastext(2) featurecur = gp.updatecursor(pfeatureclass, "") featurerow = featurecur.next() while featurerow: name_cn = featurerow.getvalue(srcfield) #print name_cn unicodenamelen = len(name_cn.decode('gbk')) asciinamelen = len(name_cn) #如果字符串中含有非中文,不处理 if unicodenamelen * 2 != asciinamelen: result = name_cn else: if unicodenamelen == 6: result = subString(name_cn, 0, 3) + "|" + subString(name_cn, 3, 6) elif unicodenamelen == 7: result = subString(name_cn, 0, 4) + "|" + subString(name_cn, 4, 7) elif unicodenamelen == 8: result = subString(name_cn, 0, 4) + "|" + subString(name_cn, 4, 8) elif unicodenamelen == 9: result = subString(name_cn, 0, 5) + "|" + subString(name_cn, 5, 9) elif unicodenamelen == 10: result = subString(name_cn, 0, 5) + "|" + subString(name_cn, 5, 10) elif unicodenamelen == 11: result = subString(name_cn, 0, 6) + "|" + subString(name_cn, 6, 11) elif unicodenamelen == 12: result = subString(name_cn, 0, 6) + "|" + subString(name_cn, 6, 12) elif unicodenamelen == 13: result = subString(name_cn, 0, 5) + "|" + subString(name_cn, 5, 10) + "|" + subString(name_cn, 10, 13) elif unicodenamelen == 14: result = subString(name_cn, 0, 5) + "|" + subString(name_cn, 5, 10) + "|" + subString(name_cn, 10, 14) elif unicodenamelen == 15: result = subString(name_cn, 0, 5) + "|" + subString(name_cn, 5, 10) + "|" + subString(name_cn, 10, 15) elif unicodenamelen == 16: result = subString(name_cn, 0, 6) + "|" + subString(name_cn, 6, 11) + "|" + subString(name_cn, 11, 16) elif unicodenamelen == 17: result = subString(name_cn, 0, 6) + "|" + subString(name_cn, 6, 12) + "|" + subString(name_cn, 12, 17) elif unicodenamelen == 18: result = subString(name_cn, 0, 6) + "|" + subString(name_cn, 6, 12) + "|" + subString(name_cn, 12, 18) else: result = name_cn featurerow.setvalue(destfield, result) featurecur.Updaterow(featurerow) gp.addwarning("Update feature row value " + result + " succeeded") featurerow = featurecur.next(); del featurecur del featurerow
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值