[Tutor] string replacements using dictionaries

dman dsh8290@rit.edu
Fri, 21 Dec 2001 14:57:20 -0500


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,
| > | 
| > | 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 :

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

Ok, I found the problem :

| <table width="98%" 
                  ^

You've got an extra % operator in there :-).

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

If you want, this could be

dataList = [ ('<li>'+s[:-1]+'</li>\n') for s in dataList]

('str' is a built-in name, so it is preferred not to use it)

| newStr = '<tr><td><ul>\n'
| 
| for str in dataList:
|     newStr = newStr + str
| 
| newStr = newStr + '</ul></td></tr>'

I don't think this is what you want :

<tr><td><ul>
foo
<tr><td><ul>
bar
</ul></td></tr>


I think you want just

newStr = '<tr><td><ul>\n' + "\n".join( dataList ) + '</ul></td></tr>'


-D

-- 

"GUIs normally make it simple to accomplish simple actions and
impossible to accomplish complex actions."
    --Doug Gwyn  (22/Jun/91 in comp.unix.wizards)