473,725 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(top rint)-1):
for j in range(0,len(top rint[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 1129
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(top rint)-1):
for j in range(0,len(top rint[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(top rint)-1):
for j in range(0,len(top rint[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(top rint)-1):
for j in range(0,len(top rint[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(topri nt)) and range(len(topri nt[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.or g> 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
2597
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 have a title followed by lines of text that do not look too ugly. If possible I would like to use an existing module. Q1) Is there such a module ?
0
1572
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 keeps generating a malformed header error sub gen_table { my $array_ref=query_data('%Ave%'); my @headings = ('id no.','street no.','street name','city'); my @rows = th(\@headings);
6
3333
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 is possible to Add and Delete rows - Some cells have special attributes (readonly and events) Here's a snippet of the code:
9
5010
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, 7, 8) ) X(foo, bar, baz); or WITH X(foo, bar, baz) AS ( VALUES (0, 1, 2), (3, 4, 5), (6, 7, 8) ) SELECT * FROM X;
8
2459
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 & email .xls files for users. these are not reports, but rather more like forms. its a pretty simple doc, two worksheets -- the first is basic info common to all recepients. the second is a simple data table that, depending on the recepient, contains...
3
1482
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 first and true on the last. Nothihng complicated here. create a simple test app in VS2005 and connect to MSSQL2005 server, no problem there. I can see it in Designer and preview the 3 rows of test data I added. Now, I create a form1 and DRAG...
3
5551
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 html does work correctly in firefox. <table style="width: 100%"> <tr style="float: left"> <td>col1</td> </tr>
1
1607
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 thousands of identical rows), but they are all inserted with a regular insert statement. I have optimized it by using the multiple row insert syntax: INSERT INTO TableName (Col1, Col2) VALUES ("Some value", "Another value"), ("Some value",...
6
4277
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 boxes are generating a postback.here is a code . protected void Page_Load(object sender, EventArgs e) { int selected_question = (int)Session; if (!Page.IsPostBack) { display_blueprint(); string...
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8097
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.