[Tutor] string replacements using dictionaries

Victor R. Cardona vcardon@siue.edu
Fri, 21 Dec 2001 12:52:56 -0600


--MGYHOYXEY6WxJCY8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Fri, Dec 21, 2001 at 12:28:28PM -0600, Victor R. Cardona wrote:
> On Fri, Dec 21, 2001 at 01:16:45PM -0500, dman wrote:
> > On Fri, Dec 21, 2001 at 11:56:45AM -0600, Victor R. Cardona wrote:
> > | Hi everyone,
> > |=20
> > | I am having a bit of a problem with string replacements. I am writing=
 a
> > | cgi script that reads in a html file and subtitutes all of the '%(key=
)s'
> > | markers for the values in a dictionary. There are five such markers
> > | containing four different keys. The dictionary has values assigned for
> > | the four keys. However, when I run the script I get "TypeError: Not
> > | enough arguements for format string" Does anyone have any idea as to
> > | what I am doing wrong?
> >=20
> > Not without seeing the code.  This works fine for me :
> >=20
> > (on one line)
> > >>> print "%(foo)s %(bar)s %(spam)s %(eggs)s" %
> >         { "foo":1 , "bar":2, "eggs":4, "spam":3 }
> > 1 2 3 4
> > >>>
>=20
> Yeah, that works for me too. I have included the code below.
>=20
> <?xml version=3D"1.0" encoding=3D"UTF-8"?>
> <!DOCTYPE html
>     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
>     "DTD/xhtml1-strict.dtd">
> <html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"en" lang=3D"en">
> <head>
> <title>%(title)s</title>
> <meta name=3D"author" content=3D"Victor R. Cardona"/>
> <link rel=3D"stylesheet" href=3D"../sysconfig/styles/work.css" type=3D"te=
xt/css"/>
> </head>
>=20
> <body>
> <div align=3D"center">
> %(blank)s
> <table width=3D"98%" border=3D"1" frame=3D"void" rules=3D"none" cellspaci=
ng=3D"0" cellpadding=3D"10">
> <tr class=3D"header"><td><h2>
> %(head)s
> </h2>
> </td>
> </tr>
> %(items)s
> </table>
> %(blank)s
> </div>
> </body>
> </html>
>=20
>=20
> #!/usr/local/bin/python
> #
> # This script will read in the todo.txt fule, and the template.html files=
,=20
> # and create an html document from the two.
> import textproc
>=20
> template =3D '/usr/local/www/data/sysconfig/template2.html'
> content =3D '/usr/local/www/data/sysconfig/todo.txt'
>=20
> templateString =3D open(template, 'r').read()
> dataList =3D open(content, 'r').readlines()
>=20
> data =3D {'title':'To Do', 'head':'System Configurator -- To Do'}
>=20
> for i in range(len(dataList)):
>     str =3D dataList[i]
>     str =3D '<li>' + str[:-1] + '</li>\n'
>     dataList[i] =3D str
>=20
> newStr =3D '<tr><td><ul>\n'
>=20
> for str in dataList:
>     newStr =3D newStr + str
>=20
> newStr =3D newStr + '</ul></td></tr>'
> data['items'] =3D newStr
> data['blank'] =3D ''
>=20
> textproc.print_file(template % data)

The last line should read:

textproc.print_file(templateString % data)

-v
--=20
Victor R. Cardona
Powered by SuSE Linux 7.1 (i386) Professional
GPG key ID E81B3A1C
Key fingerprint =3D 0147 A234 99C3 F4C5 BC64  F501 654F DB49 E81B 3A1C

--MGYHOYXEY6WxJCY8
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8I4UIZU/bSegbOhwRAhGbAJsHKcj+yLkf07LsDyUbZsqBNgEKHwCfbAoi
3ldB8wFNdB5NbhD1a1EGsDE=
=ltK4
-----END PGP SIGNATURE-----

--MGYHOYXEY6WxJCY8--