python - 在 python 中使用 HTML.py 制作表格时遇到问题

标签 python html

我正在尝试制作一个表格,在每个单元格中显示不同的值,并且根据单元格的值,具有不同的bgcolor

因此,我通过使用简单的循环成功地用一列成功地完成了此操作,但我无法找到一种方法来使用具有相同模式的多列来完成此操作。我经验不足,非常感谢您的帮助。

import HTML

test_results = [
         70,
         50,
         20,
         5,
    ]

t = HTML.Table(header_row=['test'])
for new in sorted(test_results):

    #print new
    if new <=50:
        color = 'yellow'
    elif new <=100:
        color = 'blue'
    elif new <=150:
        color = 'green'
    elif new >150:
        color = 'white'
    colored_result = HTML.TableCell(new, bgcolor=color)

    t.rows.append([colored_result])
htmlcode = str(t)
print htmlcode 

这会生成一个单列表,但我想添加更多数据并拥有一个包含许多行和列的表。

最佳答案

这个

t.rows.append([colored_result])

附加一个单元格的完整行。

这个

t.rows.append([colored_result, colored_result])

将追加此单元格两次,从而创建一行 2 个单元格(相同)。

这个

colored_result = HTML.TableCell(new, bgcolor=color)
colored_result2 = HTML.TableCell(new, bgcolor='white')
t.rows.append([colored_result, colored_result2])

会将这两个单元格作为一行附加到表格中

关于python - 在 python 中使用 HTML.py 制作表格时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7027243/

相关文章:

javascript - JQuery 动画速度问题

html - 允许特定标记覆盖溢出 :hidden

python - 使用计算机视觉进行字母表分类

python - GAE Python webapp2图片上传错误

Python Lxml - 用新数据附加现有的 xml

python - Numpy/Polyfit - 禁止打印英特尔 MKL 错误消息

html - Angular Material 选项卡动画不起作用

python - 如何在matplotlib中先绘制线,最后绘制点

javascript - bootstrap-select 下拉菜单被另一个下拉菜单隐藏

html - 在 Web 浏览器上使用 .otf 字体