将 pandas 的 'to_html' 保存为文件

11 投票
3 回答
19184 浏览
提问于 2025-04-17 16:00

我有一个叫做'tsod'的数据框,现在我想把它转换成HTML格式:

tsod.to_html()

我该怎么把这个保存成一个文件呢?最好是保存成一个'.html'文件。

3 个回答

-1
a=tsod.to_html()
save(a,'path')

将会有效

5

在当前版本的pandas中,tsod.to_html('out.html') 这个命令可以正常使用。

20
with open('my_file.html', 'w') as fo:
    fo.write(tsod.to_html())

或者可以使用pandas这个库

tsod.to_html(open('my_file.html', 'w'))

或者再次感谢@andy-hayden的建议

with open('my_file.html', 'w') as fo:
    tsod.to_html(fo)

撰写回答