Alternative dictionary output format?

Andrew M. Kuchling akuchlin at
Fri Jun 4 19:52:31 CEST 1999

Evan Simpson writes:
>I don't know if this is 100% bulletproof, but it has simple behavior:
>pctsub = re.compile('%(?!%*\()').sub
>def pfstr(s, pctsub = pctsub):
>  '''Double all '%' except those leading up to a '(', for dict formatting'''
>  return pctsub('%%', s)

An alternative is re.sub('%([^(]|\Z)', r'%%\1', <string>) .  The [^(]
| \Z will match either any character that's not a (, or at the end of
the string.  The \Z is needed in order to handle a trailing '%' --
regexes are tricky.

A.M. Kuchling
You mustn't kill me. You don't love me. You d-don't even know me.
    -- The Furies kill Abel, in SANDMAN #66: "The Kindly Ones:10"

More information about the Python-list mailing list