%* control characters question.
Richard Jones
Richard.Jones at fulcrum.com.au
Wed Dec 22 16:17:54 EST 1999
[Gerrit Holl]
> is there a module to fetch %* control characters, like strftime does?
strftime is a wrapper around the C function of the same name.
> Currently, I have this code:
>
> s=raw_input("format: ")
> string.replace(s, '%a', foo)
> string.replace(s, '%b', bar)
> string.replace(s, '%c', foobar)
> etc.
>
> I also want '%4a' to work. Are there functions to parse this sort of things?
Not as far as I know...
Would the %(foo)s syntax help? As in:
>>> "%(foo)s %(bar)s"%{'bar':'world', 'foo':'hello'}
'hello world'
>>> "%(foo)s %(bar).3s"%{'bar':'world', 'foo':'hello'}
'hello wor'
Perhaps using an re.sub on %a and %b?
>>> import re
>>> s="%a %.3b"
>>> re.sub(r"%([^a-zA-Z]*)?a", r"%(foo)\1s", s)
'%(foo)s %b'
>>> re.sub(r"%([^a-zA-Z]*)?b", r"%(bar)\1s", _)
'%(foo)s %(bar).3s'
>>> _%{'bar':'world', 'foo':'hello'}
'hello wor'
Richard
More information about the Python-list
mailing list