problem generating rows in table

Discussion in 'Python' started by s99999999s2003@yahoo.com, Nov 8, 2005.

  1. Guest

    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
    , Nov 8, 2005
    #1
    1. Advertising

  2. Steve Holden Guest

    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[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/
    Steve Holden, Nov 8, 2005
    #2
    1. Advertising

  3. Guest

    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.

    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[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, 2005
    #3
  4. Mike Meyer Guest

    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[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
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Nov 8, 2005
    #4
  5. Guest

    thanks for all the help. problem solved by taking out range().
    , Nov 8, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,218
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  2. Rio
    Replies:
    4
    Views:
    1,181
  3. PT
    Replies:
    1
    Views:
    391
    Ken Schaefer
    Oct 7, 2004
  4. eBob.com
    Replies:
    5
    Views:
    474
    Martin Honnen
    May 20, 2008
  5. T.G.
    Replies:
    2
    Views:
    1,063
Loading...

Share This Page