开发者

html table issue with for-loops

I'm having a difficulty formatting a table in html through python. I'm using for-loops from previous information, and the first two columns have been formatted, but I'm having difficulty placing certain values in the right place.

the part of my code determining the second column:

for c in sorted(max_films):
    print "<tr><th>"
    print c
    print "<td>"
    print max_films[c]
    print "</td></tr>"

should be producing the third

for c in max_list1:
    print "<tr><th>"
    print "<td>"
    print c
    print "</td></tr>"

This is the table it is producing:

  • Year Highest Grossing Film Most Similar 2nd Most Similar 3rd Most Similar
  • 2000 The Sixth Sense
  • 2001 Star Wars: Episode I - The Phantom Menace
  • 2002 Harry Potter and the Sorcerer's Stone
  • 2003 The Lord of the Rings: The Two Towers
  • 2004 The Lord of the Rings: The Return of the King
  • 2005 Star Wars: Episode III - Revenge of the Sith
  • 2006 Pirates of the Caribbean: Dead Man's Chest
  • 2007 Pirates of the Caribbean: At World's End
  • (blank)The Perfect Storm
  • (blank)Planet of the Apes
  • (blank)The Lord of the Rings: The Fellowship of the Ring
  • (blank)Spirited Away
  • (blank)Collateral
  • (blank)Troy
  • (blank)The Chronicles of Narnia: The Lion the Witch and the Wardrobe
  • (blank)300

The films(Perfect Storm - 300) should be aligned in the third column instead of under the second. How can i fix this??

full html code:

print "Content-Type: text/html"
print ""
print "<html>"
print "<body>"
print "<table border=1>"

print "<tr>"
print "<th><font color=black>Year</font></th>"
print "<th><font color=blue>Highest Grossing Fi开发者_如何学JAVAlm</font></th>"
print "<th><font color=red>Most Similar</font></th>"
print "<th><font color=red>2nd Most Similar</font></th>"
print "<th><font color=red>3rd Most Similar</font></th>"
print "<th><font color=red>4th Most Similar</font></th>"
print "</tr>"


for c in sorted(max_films):
    print "<tr><th>"
    print c
    print "<td>"
    print max_films[c]
    print "</td></tr>"


for c in max_list1:
    print "<tr><th>"
    print "<td>"
    print c
    print "</td></tr>"

print "</body>"
print "</html>"

sorted(max_films) = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007']

max_list1 = ['The Perfect Storm', 'Planet of the Apes', 'The Lord of the Rings: The Fellowship of the Ring', 'Spirited Away', 'Collateral', 'Troy', 'The Chronicles of Narnia: The Lion the Witch and the Wardrobe', '300']


Errors

  • You should not use table rows (tr elements) for columns!
  • You do not close open headers (th elements)
  • You should use table headers in the head onlym not in the content rows!

Valid table structure

    <table>
        <tr>
            <th>Year</th>
            <th>Highest Grossing Film</th>
            <th>Most Similar</th>
            <th>2nd Most Similar</th>
            <th>3rd Most Similar</th>
        </tr>

    <!-- create rows in a loop -->
    <tr>
        <td> ... </td>
        <td> ... </td>
        <td> ... </td>
        <td> ... </td>
        <td> ... </td>
    </tr>
    <!-- loop this ^^ -->

</table>

See sample: http://jsfiddle.net/n2w5D/

General solution

You cannot loop over columns when creating tables, because you need to write all table cells of a row into one row. The structure of a table is based on rows as parent element not on columns! You will need to have the data for each row to procude something like this:

print "<table>"

print "<tr>"
    // print your caption row
    print "<th> Year </th>"
    print "<th> Highest Grossing Film </th>"
    print "<th> Most Similar </th>"
    print "<th> 2nd Most Similar </th>"
    print "<th> 3rd Most Similar </th>"
print "</tr>"

// loop data
for row_data in data:
    print "<tr>"

    // print data (maybe: row_data[0] == year, row_data[1] == movie name etc.)
    for cell_data in row_data:
        print "<td>"
        print cell_data
        print "</td>"

    print "</tr>"

print "</table>"

Solution for your case

After you posted your full code, I think you didn't mean columns, but rows! I think this is what you want:

for c in sorted(max_films):
    print "<tr><td>"
    print max_films[c]
    print "</td>"

for c in max_list1:
    print "<td>"
    print c
    print "</td></tr>"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜