【问题标题】:How to capture jupyter cell output without supressing live printing?如何在不抑制实时打印的情况下捕获 jupyter 单元输出?
【发布时间】:2021-02-11 11:00:15
【问题描述】:

在 jupyter 中有一个神奇的函数可以捕获和解析单元格的输出:

%%capture capt_out
# cell body that prints interesting stuff every second
# another cell
# code for printing or processing output (i.e for plots):
print(capt_out)

问题是,我需要实时查看输出(因为这是一个暗网模型训练,我需要知道已经取得了多少进展),然后对其进行解析以创建基于它的绘图。

有没有一种简单的方法可以在不抑制打印的情况下捕获输出?

【问题讨论】:

  • 问题实际上与machine-learning 无关 - 请不要向无关标签发送垃圾邮件(已删除)。
  • 好吧抱歉,不是故意的

标签: python jupyter-notebook output capture


【解决方案1】:

这不是一个通用的解决方案,但适用于我的情况:

!./darknet detector some_parameters 2>&1 | tee some_file.txt

由于我的 jupyter 单元通过 bash 运行二进制文件,因此我可以简单地使用 tee 命令将输出保存到文件中,而不会抑制实时打印。然后我用python读取了这个文件

with open(some_file.txt, r) as f:
   text = f.read()

2>&1 将标准错误重定向到标准输出。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-12
    • 2022-08-19
    • 2020-09-03
    • 2020-05-29
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多