Invalid format character in string

Robin Munn rmunn at pobox.com
Sat Apr 5 12:59:42 EST 2003


Adam Hupp <hupp at cs.wisc.edu> wrote:
> On Sat, Apr 05, 2003 at 12:17:52AM +0200, Nikolai Kirsebom wrote:
>> Hi,
>> The problems arise where the HTML editor used (to produce the file in
>> the first place) has put various expressions of %; and %>.  These are
>> HTML codings the creator of the HTML file does not really know about.
>> As an example, I've just told them (the users creating the HTML file)
>> that they should write the text "%(name)s" where they want the name
>> generated and "%(age)s" where they want the age generated.  Then my
>> script is run for a set of person objects producing a set of HTML
>> files with the correct names and corresponding ages. 
> 
> You could apply this regexp to the string before you do the insertion:
> 
>>> re.sub('%(?!\(\w+\)s)','%%','%foo %(bar)s')
><< '%%foo %(bar)s'
> 
> The '(?!...)' is a negative lookahead assertion.

I'd suggest stripping the "s" off the end of that regexp, myself: you
never know if someone might have decided they wanted to print a decimal
number or whatever with "%(age)d". Yes, he told the users to use s, but
one of them might know a little C programming and decide "Oh, this looks
like printf() format strings, in which case that 's' should be a 'd'."

A little constructive paranoia often comes in handy, I find...

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list