Invalid format character in string

Adam Hupp hupp at cs.wisc.edu
Sat Apr 5 00:42:20 CEST 2003


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.

-Adam





More information about the Python-list mailing list