从Python中的列表生成html表
我目前有一个数据列表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法连接字符串和浮点类型,您需要将浮点类型转换为字符串:
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
或者
如果您想打印整数部分加 2 位小数
http://docs. python.org/library/stdtypes.html#string-formatting-operations
or
if you want to print integer part plus 2 decimals
http://docs.python.org/library/stdtypes.html#string-formatting-operations