for循环的html表格问题

发布于 2024-12-09 06:45:17 字数 2237 浏览 0 评论 0原文

我在通过 python 格式化 html 表格时遇到困难。我正在使用以前信息中的 for 循环,并且前两列已格式化,但我很难将某些值放置在正确的位置。

我的代码确定第二列的部分:

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>"

这是它正在生成的表格:

  • 年份最高票房电影最相似第二最相似第三最相似
  • 2000第六感
  • 2001星球大战:第一集 - 幽灵威胁
  • 2002哈利·波特与魔法石
  • 2003 指环王:双塔奇兵
  • 2004 指环王:归来国王之王
  • 2005 星球大战前传 III - 西斯的复仇
  • 2006 加勒比海盗:聚魂箱
  • 2007 加勒比海盗:世界的尽头
  • (空白)完美风暴
  • (空白)猩球崛起
  • (空白)领主指环王:指环王
  • (空白)千与千寻
  • (空白)附属品
  • (空白)特洛伊
  • (空白)纳尼亚传奇:狮子、女巫和魔衣橱
  • (空白)300

电影(完美风暴 - 300)应在第三列中对齐,而不是在第二列下对齐。我该如何解决这个问题?

完整的html代码:

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 Film</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']

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 Film</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']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-12-16 06:45:17

错误

  • 您不应该使用表行(tr 元素)作为列!
  • 您不关闭打开的标题(th 元素)
  • 您应该仅在标题中使用表标题,而不是在内容行中!

有效的表结构

    <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>

请参阅示例: http://jsfiddle.net/n2w5D/

一般解决方案

您无法在以下情况下循环列:创建表格,因为您需要将一行的所有表格单元格写入一行。表的结构基于行作为父元素而不是列!
您将需要每行的数据才能产生如下所示的内容:

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>"

您的案例的解决方案

在发布完整代码后,我认为您指的不是列,而是行!
我想这就是你想要的:

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>"

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>"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文