views:

112

answers:

1

I have a PSP page with html embedded. I need to place another for loop so i can insert another %s next to background-color: which will instert a appropriate colour to colour in the html table.

For example i need to insert for z in colours so it can loop over the colours list and insert the correct colour. Where ever i try to insert the for loop it doesnt seem to work it most commonly colours each cell in the table 60 times then moves onto the next cell and repeats itself and crashes my web browser.

The colours are held in a table called colours.

code below:

<table>
<%
s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
    req.write('<tr><td><TT>%04d</td>' % (i+1));
    for k in s[i:i+60]:
            req.write('<TT><td><TT><font style="background-color:">%s<font></td>' % (k));
    req.write('</TT></tr>')
#end
%>
</table>

-----EDITED-----

Plugged in the code provided ebo, it colours the table all one colour. The colours list contains a variety of colours e.g. colour = ['yellow', 'yellow', 'yellow', 'yellow', 'red', 'red', 'red', 'red']

<table>
<%
s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
    req.write('<tr><td>%04d</td>' % (i+1));
    for j, k in enumerate(s[i:i+60]):
        req.write('<td><font style="background-color:%s;">%s<font></td>' % (colour[j % len(colour)], k));
    req.write('</tr>')
#end
%>
</table>
+1  A: 

I guess you want one color for each column. Best idea would be to use enumerate:

s = ''.join(aa[i] for i in table if i in aa)
for i in range(0, len(s), 60):
    req.write('<tr><td>%04d</td>' % (i+1))
    for j, k in enumerate(s[i:i+60]):
        req.write('<td style="background-color: %s;">%s</td>' % 
                     (colours[j % len(colours)], k))
    req.write('</tr>')

I stripped all the TT tags. They were mostly wrong, either not closed or spanning over other elements.

Update This should do. Take a look at the source, if every field is filled correctly. Also download Firebug and take a look at the parsed html code. It may differ depending on your other html failures.

colour = ["red", "red", "green", "yellow"]

print "<table>"
s = '1234567890'
for i in range(0, len(s), 60):
    print('<tr><td>%04d</td>' % (i+1));
    for j, k in enumerate(s[i:i+60]):
        print('<td><font style="background-color:%s;">%s<font></td>' % (colour[j % len(colour)], k));
    print('</tr>')
print "</table>"

I piped the output of the following code into a html file and opened it. Works as expected.

python table.py > table.html
firefox table.html

I guess you have some additional errors in your code which mess up parsing.

ebo
Hey plugged your code in, the table came up as all one colour. The colour list contains a list of colours e.g. ['yellow', 'yellow', 'yellow', 'red', 'red', 'red', 'green', 'green']
James