Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 编写和显示自动生成的html代码_Python_Html_Python 2.7 - Fatal编程技术网

Python 编写和显示自动生成的html代码

Python 编写和显示自动生成的html代码,python,html,python-2.7,Python,Html,Python 2.7,我正在从python列表生成html代码 import webbrowser food_list = [(u'John Doe', 1.73),(u'Jane Doe', 1.73), (u'Jackie O', 1.61)] def print_html(lista): print '<table>' for i in food_list: print '<tr>' print '<td>'+str(i[

我正在从python列表生成html代码

import webbrowser

food_list = [(u'John Doe', 1.73),(u'Jane Doe', 1.73), (u'Jackie O', 1.61)]


def print_html(lista):
    print '<table>'
    for i in food_list:
        print '<tr>'
        print '<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>'
        print '</tr>'
    print' </table>'

code = print_html(food_list)
print code

h = open('list.html','w')

h.write(code)
h.close()

webbrowser.open_new_tab('list.html')
我不想硬编码html代码

它一定很简单,我想不出来


谢谢您的建议。

您可以尝试使用Python自己的html包:


应该是这样的代码

因为您打印了变量,所以没有返回任何内容。由于哪个原因出现了错误

import webbrowser

food_list = [(u'John Doe', 1.73),(u'Jane Doe', 1.73), (u'Jackie O', 1.61)]


def print_html(food_list):
    a=""
    a=a+'<table>\n'
    for i in food_list:
        a=a+'<tr>\n'
        a=a+'<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>\n'
        a=a+'</tr>\n'
    a=a+' </table>\n'
    return a

code = print_html(food_list)
print str(code)

h = open('list.html','w')

h.write(str(code))
h.close()

webbrowser.open_new_tab('list.html')
现在运行它并查看


它首先显示了错误,因为代码变量属于none类型

您没有返回任何内容,因此尝试写入none,请在传递文件名和列表的函数中写入:

def write_html(lista,f):
    with open(f, "w") as f:
        f.write('<table>')
        for i in lista:
            f.write('<tr>')
            f.write('<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>')
            f.write( '</tr>')
        f.write('</table>')

write_html(food_list,"list.html")
webbrowser.open_new_tab('list.html')
您可以连接然后编写,但是如果您的目标是只编写,那么在迭代列表时编写就更容易了

如果运行代码,您将在末尾看到返回值:

In[27]:打印优先级 打印html

In [27]: print print_html(food_list)
<table>
<tr>
<td>John Doe</td><td>1.73</td>
</tr>
<tr>
<td>Jane Doe</td><td>1.73</td>
</tr>
<tr>
<td>Jackie O</td><td>1.61</td>
</tr>
 </table>
None # what you are trying to write
或者使用python2语法:

def write_html(lista,f):
    with open(f, "w") as f:
        print >>f, '<table>'
        for i in lista:
            print >>f,'<tr>'
            print >>f, '<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>'
            print >>f, '</tr>'
        print >>f,' </table>'

您是否打印了代码并看到代码中没有任何内容,因为您只是在打印而没有返回。您试图编写的代码没有任何内容,这是您的函数返回的内容。您的代码更面向模块。这是一种解决方法,而不是解释代码为什么不工作。您是对的。我会编辑我的回复,但我认为他现在有很多反馈。
John Doe    1.73
Jane Doe    1.73
Jackie O    1.61
In [27]: print print_html(food_list)
<table>
<tr>
<td>John Doe</td><td>1.73</td>
</tr>
<tr>
<td>Jane Doe</td><td>1.73</td>
</tr>
<tr>
<td>Jackie O</td><td>1.61</td>
</tr>
 </table>
None # what you are trying to write
def write_html(lista,f):
    with open(f, "w") as f:
        print('<table>',file=f)
        for i in lista:
            print('<tr>',file=f)
            print('<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>',file=f)
            print( '</tr>',file=f)
        print(' </table>',file=f)
def write_html(lista,f):
    with open(f, "w") as f:
        print >>f, '<table>'
        for i in lista:
            print >>f,'<tr>'
            print >>f, '<td>'+str(i[0]) +'</td>''<td>'+str(i[1])+'</td>'
            print >>f, '</tr>'
        print >>f,' </table>'