0

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>
5
  • 2
    By the way, you are producing really bad HTML code. :-( Commented Mar 12, 2010 at 13:41
  • I know i just want a working page at the moment... Commented Mar 12, 2010 at 13:41
  • 4
    A working page will include valid html code. Please fix your html first and add an example for colours. Commented Mar 12, 2010 at 13:44
  • 1
    What is the intermediate for k doing? Which number is meant to be an index into the colours table? Is it just i? In that case it should be colours[i % len(colours)]. Commented Mar 12, 2010 at 14:31
  • 1
    But what's s? What does it have to do with colours? Can you be more precise about the ordering of the sequence you want to get out? Commented Mar 12, 2010 at 16:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.