[Tutor] string replacements using dictionaries

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

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

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?
> Not without seeing the code.  This works fine for me :
> (on one line)
> >>> print "%(foo)s %(bar)s %(spam)s %(eggs)s" %
>         { "foo":1 , "bar":2, "eggs":4, "spam":3 }
> 1 2 3 4
> >>>

Yeah, that works for me too. I have included the code below.

<?xml version=3D"1.0" encoding=3D"UTF-8"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns=3D"http://www.w3.org/1999/xhtml" xml:lang=3D"en" lang=3D"en">
<meta name=3D"author" content=3D"Victor R. Cardona"/>
<link rel=3D"stylesheet" href=3D"../sysconfig/styles/work.css" type=3D"text=

<div align=3D"center">
<table width=3D"98%" border=3D"1" frame=3D"void" rules=3D"none" cellspacing=
=3D"0" cellpadding=3D"10">
<tr class=3D"header"><td><h2>

# This script will read in the todo.txt fule, and the template.html files,=
# and create an html document from the two.
import textproc

template =3D '/usr/local/www/data/sysconfig/template2.html'
content =3D '/usr/local/www/data/sysconfig/todo.txt'

templateString =3D open(template, 'r').read()
dataList =3D open(content, 'r').readlines()

data =3D {'title':'To Do', 'head':'System Configurator -- To Do'}

for i in range(len(dataList)):
    str =3D dataList[i]
    str =3D '<li>' + str[:-1] + '</li>\n'
    dataList[i] =3D str

newStr =3D '<tr><td><ul>\n'

for str in dataList:
    newStr =3D newStr + str

newStr =3D newStr + '</ul></td></tr>'
data['items'] =3D newStr
data['blank'] =3D ''

textproc.print_file(template % data)

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

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

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