我目前有一个数据列表:
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
我真的不明白我应该如何处理这个问题。
答案 0 :(得分:2)
答案 1 :(得分:2)
或
print ' <td>%.2f</td>' % i[1]
如果要打印整数部分加上2位小数
http://docs.python.org/library/stdtypes.html#string-formatting-operations