problem generating rows in table

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • s99999999s2003@yahoo.com

    #1

    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

  • Steve Holden

    #2
    Re: problem generating rows in table

    s99999999s2003@ yahoo.com wrote:[color=blue]
    > 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
    >[/color]
    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/

    Comment

    • bonono@gmail.com

      #3
      Re: problem generating rows in table

      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.

      s99999999s2003@ yahoo.com wrote:[color=blue]
      > 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[/color]

      Comment

      • Mike Meyer

        #4
        Re: problem generating rows in table

        s99999999s2003@ yahoo.com writes:[color=blue]
        > 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[/color]

        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 <mwm@mired.or g> http://www.mired.org/home/mwm/
        Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

        Comment

        • s99999999s2003@yahoo.com

          #5
          Re: problem generating rows in table

          thanks for all the help. problem solved by taking out range().

          Comment

          Working...