Python取子字符串方法
要求:从字符串中
sfsfsd345#-3sfsfsd3.4me取345#-3出来Python自定义函数的写法如下所示:
>>> ================================ RESTART ================================ >>> def getsubstr(txt_s): #处理换行符问题 t=txt_s findex=txt_s.find('#') s_len=len(txt_s) t=t[0:findex] dkbh='' bh_s='' for a in t: #获取是数字 中划线 下划线的子字符串 if(a.isdigit()==True or a=='-' or a=='_'): if(dkbh==''): dkbh=a else: dkbh+=a s=txt_s[findex+1:findex+2] if(s=='-'): s=txt_s[findex+1:s_len] for b in s: if(b.isdigit()==True or b=='-' or b=='_'): if(bh_s==''): bh_s=b else: bh_s+=b else: break dkbh+='#'+bh_s return dkbh >>> print getsubstr('sfsfsd345#-3sfsfsd3.4men') 345#-3 >>>
>>> ================================ RESTART ================================ >>> def getAreaVal(txt_s): findex=txt_s.find('#') s_len=len(txt_s) rb='' bh_s='' s=txt_s[findex+1:findex+2] if(s=='-'): s=txt_s[findex+1:s_len] for b in s: if(b.isdigit()==True or b=='-' or b=='_'): if(bh_s==''): bh_s=b else: bh_s+=b else: break q=txt_s[findex+1:s_len] for c in q: if(c.isdigit()==True or c=='-' or c=='_' or c=='.'): if(rb==''): rb=c else: rb+=c else: continue return rb.replace(bh_s,'') >>> print getAreaVal('sfsdfd45#-2sfdfs45.6men') 45.6 >>>