String + number split
William Park
opengeometry at yahoo.ca
Mon Apr 12 18:05:05 EDT 2004
Stevie_mac <no.email at please.com> wrote:
> Heres my solution...
>
> import string
> def makefont(sFontName):
> num = []
> nam = []
> acnt = 0
> #reverse it
> l = list(sFontName); l.reverse(); ''.join(l)
> sFontName = string.join(l,'')
> #now loop it while isdigit(), store number, then store alphas
> for c in sFontName:
> if c.isdigit() and acnt == 0:
> num.append(c)
> elif c.isalpha() or acnt > 1:
> acnt += 1
> nam.append(c)
> nam.reverse()
> num.reverse()
> return (string.join( nam, '' ), int(string.join( num, '' )))
>
> Now you see why i was asking for a more elegant solution!
>
> PS, the number on the end may vary & the _ could be any non alpha char!
>
> font12 becomes ('font',12)
> arial_14 becomes ('arial',14)
> arial__8 becomes ('arial',8)
> times 6 becomes ('times',6)
Assuming you only have 2 fields to worry about, play around with
1. re.split('[^a-z0-9]+', '...')
2. re.findall('[a-z]+|[0-9]+', '...')
Essentially, you want to pickout '[a-z]+' first and then '[0-9]+', ie.
([a-z]+)[^0-9]+([0-9]+)
--
William Park, Open Geometry Consulting, <opengeometry at yahoo.ca>
Linux solution/training/migration, Thin-client
More information about the Python-list
mailing list