problem generating rows in table

S

s99999999s2003

hi
i wish to generate a table using cgi
toprint = [('nickname', 'justme', 'someplace')]
print '''<table border="1">
<tr>
<td>User</td>
<td>Name</td>
<td>Address</td>
</tr>
<tr>
'''

for i in range(0,len(toprint)-1):
for j in range(0,len(toprint[0])-1):
print "<td> %s </td>" % toprint[j]

print '''</tr>
</table>'''

but it only prints out a table with "User | Name | address"
it didn't print out the values of toprint

is there mistake in code? please advise
thanks
 
S

Steve Holden

hi
i wish to generate a table using cgi
toprint = [('nickname', 'justme', 'someplace')]
print '''<table border="1">
<tr>
<td>User</td>
<td>Name</td>
<td>Address</td>
</tr>
<tr>
'''

for i in range(0,len(toprint)-1):
for j in range(0,len(toprint[0])-1):
print "<td> %s </td>" % toprint[j]

print '''</tr>
</table>'''

but it only prints out a table with "User | Name | address"
it didn't print out the values of toprint

is there mistake in code? please advise
thanks

Your problem is in trying to emulate the C looping structures rather
than using those native to Python: the toprint list has only one
element, and the range computations suffer from out-by-one errors,
leaving you iterating zero times!

It might be simpler to build the output as follows:

print '''<table border="1">
<tr>
<td>User</td>
<td>Name</td>
<td>Address</td>
</tr>
'''
rows = []
for row in toprint:
print " <tr>"
for cell in row:
print " <td>%s</td>" % cell
print " </tr>"
print "</table>"

Of course you should really be ensuring that the cell contents correctly
escape any special characters in the cell content (such as turning "<"
into "&lt;") but I'll leave that as an exercise.

regards
Steve
 
B

bonono

len(toprint) -1 seems to be 0 so it seems that the loops are skipped ?

why not just :
for x in toprint:
for y in x:
print "<td>%s</td>" % y

these suffix things are very prone to error.
 
M

Mike Meyer

hi
i wish to generate a table using cgi
toprint = [('nickname', 'justme', 'someplace')]
print '''<table border="1">
<tr>
<td>User</td>
<td>Name</td>
<td>Address</td>
</tr>
<tr>
'''

for i in range(0,len(toprint)-1):
for j in range(0,len(toprint[0])-1):
print "<td> %s </td>" % toprint[j]

print '''</tr>
</table>'''

but it only prints out a table with "User | Name | address"
it didn't print out the values of toprint

is there mistake in code? please advise


You're not calling range right. It's designed for dealing with lists,
so range(n) returns [0, ..., n - 1], and range(to, bottom) returns
[top, ..., bottom - 1]. len(toprint) is 1, so len(toprint) - 1 is 0,
so you're calling range(0, 0), which is an empty list. So you make no
passes through the outer loop. Those two calls to range should be
range(len(toprint)) and range(len(toprint)) (n.b.: i, not 0, is
the index).

Of course, for is for walking elements of a list. You don't need the
indices at all. The pythonic way to write this loop would be:

for tup in toprint:
for el in tup:
print "<td> %s </td>" % el

But your HTML is also broken. The loop as you have it will print one
row containing all the elements in toprint concatenated together. You
need to put each tuple in toprint into a separate row, like so:

for tup in toprint:
print "<tr>"
for el in tup:
print "<td> %s </td>" % el
print "</tr>"
print "</table>"

and of course leave out the trailing <tr> in the print statement that
precedes the loop.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top