将python列表转换为HTML表

时间:2016-05-26 05:51:08

标签: python html

我是新手编码员,我无法弄清楚我的错误...... 我的文字看起来像这样:

AAA,48,48,0,0,0,0,0,0,0,0,0
BBB,30,24,6,0,6,0,0,0,0,0,0

我想将它写入HTML表格。代码是这样的:

temp_lines=ftemp.readlines();
for line in temp_lines:
        tl,total,counter_pass,counter_kbug,counter_ubug,totbug,counter_kscr,counter_uscr,totscr,counter_kset,counter_uset,totset=line.split(",");

f.write("<tr><td height=\"30\"><a href="something".html>"+str(t1)+"</a></td><td height=\"30\">"+str(total)+"</a></td><td height=\"30\">"+str(counter_pass)+"</td><td height=\"30\">"+str(counter_kbug)+"</td><td>"+str(counter_ubug)+"</td><td height=\"30\">"+str(totbug)+"</td><td height=\"30\">"+str(counter_kscr)+"</td><td height=\"30\">"+str(counter_uscr)+"</td><td height=\"30\">"+str(totscr)+"</td><td height=\"30\">"+str(counter_kset)+"</td><td height=\"30\">"+str(counter_uset)+"</td><td height=\"30\">"+str(totset)+"</td></tr></center><br>");

我收到此错误:

TypeError: cannot concatenate 'str' and 'list' objects

我明确地将每个列表对象转换为字符串。我哪里错了?

1 个答案:

答案 0 :(得分:0)

考虑following

For example, ' 1  2   3  '.split() returns ['1', '2', '3']

你正试图做这个str(['1','2','3'])

尝试连接时会发生什么:

"some html" + str(['1', '2', '3'])"
TypeError: cannot concatenate 'str' and 'list' objects

考虑阅读list manipulation或查看this上的教程:

基本上,您需要从列表中提取数据:

var_to_html = List_name[0] #position of the element to extract from the list
 now you can do "html" + var_to_html