【问题标题】:How to print cell output to a file on colab如何将单元格输出打印到colab上的文件
【发布时间】:2021-07-24 05:26:35
【问题描述】:

在 Google Colab 中,我想执行几行 python 代码,包括打印和函数。目标是将结果重定向文件。有什么提示吗?

【问题讨论】:

    标签: python-3.x jupyter-notebook google-colaboratory


    【解决方案1】:
    1. 在单元格中您要打印到文件中吗?写下以下内容:
    %%capture cap
    
    1. 要打印到文件中:
    f = open("output.txt", "w") 
    print(cap, file=f)
    f.close()
    
    • 注意:对于非常大的输出,它可能无法很好地工作。 变量c的对象类型是来自IPython.utils.capture的CapturedIO

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多