【问题标题】:save pandas' to_html' as a file将 pandas 的 to_html 保存为文件
【发布时间】:2013-01-31 14:42:36
【问题描述】:

我有一个 DateFrame 'tsod',现在我将其转换为 html:

tsod.to_html()

如何将其保存为文件?最好保存为“.html”文件。

【问题讨论】:

    标签: python pandas


    【解决方案1】:
    with open('my_file.html', 'w') as fo:
        fo.write(tsod.to_html())
    

    或者使用熊猫

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

    或再次(感谢@andy-hayden)

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

    【讨论】:

    • 我认为tsod.to_html('my_file.html') 不会起作用。 to_html 不接受字符串,它接受可以写入的内容,无论是打开的文件还是 StringIO 缓冲区(它会查找 .write())。不过,您可以使用tsod.to_html(fo)
    • 使用 StringIO 可能是解决此问题的另一种方法...将 html 写入字符串,将字符串转储到文件中。
    • 我认为这里的好做法是仍然使用 with around the pandas bit:tsod.to_html(fo).
    • 现在碰巧找到这个的人,to_html 现在确实接受了字符串,所以将其命名为 tsod.to_html('Name.html')
    【解决方案2】:
    a=tsod.to_html()
    save(a,'path')
    

    会起作用

    【讨论】:

    • 什么是save()?这不是 python 内置函数 - 提示,请尝试添加 import 语句。
    【解决方案3】:

    截至当前版本的 pandas tsod.to_html('out.html') 工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 2019-02-06
      • 2018-05-31
      • 2019-01-29
      • 2018-11-21
      • 2020-08-02
      • 2019-03-14
      相关资源
      最近更新 更多