将pandas'to_html'保存为文件

wuw*_*cat 6 python pandas

我有一个DateFrame'tsod',现在我把它转换为html:

tsod.to_html()
Run Code Online (Sandbox Code Playgroud)

如何将其保存为文件?最好保存为'.html'文件.

dan*_*van 11

with open('my_file.html', 'w') as fo:
    fo.write(tsod.to_html())
Run Code Online (Sandbox Code Playgroud)

或者使用熊猫

tsod.to_html(open('my_file.html', 'w'))
Run Code Online (Sandbox Code Playgroud)

或者再次(谢谢@ andy-hayden)

with open('my_file.html', 'w') as fo:
    tsod.to_html(fo)
Run Code Online (Sandbox Code Playgroud)

  • 我不认为`tsod.to_html('my_file.html')`会起作用.`to_html`不接受字符串,它接受它可以写入的内容,无论是打开文件还是StringIO缓冲区(它查找`.write()`).但是你可以使用`tsod.to_html(fo)`. (2认同)

Abb*_*bas 5

截至当前版本,pandastsod.to_html('out.html')工作正常。