从Python中的列表生成html表

发布于 2024-11-09 05:50:49 字数 803 浏览 0 评论 0原文

我目前有一个数据列表:

    list = [('ALCOA INC. CDI', 66.0), ('RIO TINTO LIMITED', 29.210000000000001), ('PLACER DOME INC CDI', 18.030000000000001), ('GUNNS LIMITED', 11.609999999999999), ('ORICA LIMITED', 10.83)] 

这是从我的代码生成的,我想将其放入 html 表中,目前我可以让它仅将公司名称输入到表中,当我尝试输入数字时就是不行。

    print '    <table border = "1" cellpadding = "1" cellspacing = "0">'
    print '         <tr>'
    for i in list:
        print '        <td>'+i[1]+'</td>' #here is where i've tried to only input the numbers, but output isn't working.
    print '            </td>'
    print '        </tr>'
    print '    </table>'
TypeError: cannot concatenate 'str' and 'float' objects

我真的不明白我应该如何解决这个问题。

I currently have a list of data:

    list = [('ALCOA INC. CDI', 66.0), ('RIO TINTO LIMITED', 29.210000000000001), ('PLACER DOME INC CDI', 18.030000000000001), ('GUNNS LIMITED', 11.609999999999999), ('ORICA LIMITED', 10.83)] 

that's what's generated from my code, and I want to place it into a html table, currently I can get it to only input the company name into the table, when i try to put in the numbers it just doesn't work.

    print '    <table border = "1" cellpadding = "1" cellspacing = "0">'
    print '         <tr>'
    for i in list:
        print '        <td>'+i[1]+'</td>' #here is where i've tried to only input the numbers, but output isn't working.
    print '            </td>'
    print '        </tr>'
    print '    </table>'
TypeError: cannot concatenate 'str' and 'float' objects

I don't really understand how I should be going about this problem.

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

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

发布评论

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

评论(2

溺深海 2024-11-16 05:50:49

您无法连接字符串和浮点类型,您需要将浮点类型转换为字符串:

print ' '+str(i[1])+''

http://ideone.com/I0LJV

You can't concatenate string and float types, you need to cast the float to a string:

print ' <td>'+str(i[1])+'</td>'

http://ideone.com/I0LJV

长梦不多时 2024-11-16 05:50:49

或者

print '        <td>%.2f</td>' % i[1]

如果您想打印整数部分加 2 位小数

http://docs. python.org/library/stdtypes.html#string-formatting-operations

or

print '        <td>%.2f</td>' % i[1]

if you want to print integer part plus 2 decimals

http://docs.python.org/library/stdtypes.html#string-formatting-operations

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