473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem generating rows in table

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[i][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

Nov 8 '05 #1
4 1119
s9************@yahoo.com wrote:
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[i][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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 8 '05 #2
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.

s9************@yahoo.com wrote:
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[i][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


Nov 8 '05 #3
s9************@yahoo.com writes:
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[i][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[i])) (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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 8 '05 #4
thanks for all the help. problem solved by taking out range().

Nov 8 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Philippe C. Martin | last post by:
Hi, I wish to use an easy way to generate reports from wxPython and feel wxHtmlEasyPrinting could be a good solution. I now need to generate the HTML wxHtmlEasyPrinting can print: I need to...
0
by: smrtalec | last post by:
I am trying to scavange a example script I found on the web. The aubroutine is below. Basically I want to extract data from a sql db then generate a table of values on the fly. The script below...
6
by: Thomas | last post by:
Hi, I'm having a problem with the dynamically created inputfields in Internet Explorer. The situation is the following: - I have a dynamically created table with a textbox in each Cell. - It...
9
by: YONETANI Tomokazu | last post by:
Hi. You can use the following SQL to construct rows with column names on the fly, rather than from an existing table like sysibm.sysdummy1: SELECT * FROM TABLE ( VALUES (0, 1, 2), (3, 4, 5), (6,...
8
by: matt | last post by:
hello, can anyone speak to some of the common or preferred methods for building Excel .XLS files, programmatically thru the .NET framework? i have an intranet app that needs to generate &...
3
by: Robert Johnson | last post by:
Hi all. Created a simple table in my db. 3 colums one is a Int set for autoincrement. Itentity True, seed 1, Incremement 1, null False. The other colums are simple VarChar(50) null false on the...
3
by: Harry | last post by:
Using IE7, I'm trying to display a table in a horizontal manner by floating the rows. The following html does not work, displaying the table vertically as if the rows were not floated. This same...
1
geon
by: geon | last post by:
Hi! I have a table I'm using as a stack to pop rows from. (The data in the stack is precomputed for efficiency and reliability.) There are a lot of duplicate rows in this table (hundreds or...
6
by: shashi shekhar singh | last post by:
Respected Sir, I have to create multiple dynamic dropdownlist boxes and add items dynamically in <asp:table> server control but problem occurs , i.e. except of fist dropdown list no dropdownlist...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.