sscanf ?
Bjorn Pettersen
BPettersen at NAREX.com
Tue Nov 20 14:26:04 EST 2001
> From: Bruce Edge [mailto:bedge at troikanetworks.com]
>
> In <3BFAA0FC.EB3CB91B at alcyone.com>, Erik Max Francis wrote:
>
> > Bruce Edge wrote:
> >
> >> I know about all the regex stuff, but I have the format
> string that
> >> was used to generate a string, which I'd like to use to
> decompose it
> >> back into it's source data:
> >>
> >> fmtstr = "%02d.%02d"
> >>
> >> str = fmtstr % ( x, y )
> >>
> >> # Now get x any y back using only str and fmtstr:
> >>
> >> (x, y ) = ?
> >
> > x, y = map(int, str.split('.'))
> >
>
> OK, that works for this case, I was looking for something
> more generic.
>
> What about for this case:
>
> fmtstr = "x=%02d and y is %02d"
>
> currently I'm looking at converting the %02d into \d{:2} for
> use with the re module.
Maybe the re module will help?
>>> s = 'x=%02d and y is %02d'
>>> s % (10,12)
'x=10 and y is 12'
>>> import re
>>> m = re.match(r'x=(\d+) and y is (\d+)', s % (10,12))
>>> m.groups()
('10', '12')
>>>
-- bjorn
More information about the Python-list
mailing list