【问题标题】:no output in python but output in jupyter notebookpython中没有输出,但在jupyter notebook中输出
【发布时间】:2021-06-04 16:36:19
【问题描述】:

运行以下代码

with open('abc', 'wt') as fh:
    fh.write('\n'.join(['a','b','c']))

ipython 中,没有预期的输出; 但是,在jupyter 笔记本中,输出类似于[Out 4]: 5

可能与

有关
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

在 jupyter 中。但是jupyter输出有什么奇怪的东西,如何预防呢?

【问题讨论】:

    标签: python jupyter-notebook output jupyter


    【解决方案1】:

    我不熟悉 jupyter,但如果它是一个 REPL,它会在计算表达式的结果时打印它们,它会打印 fh.write 的返回值。 write 返回写入的字符数,以便打印出来。

    _ = fh.write 阻止输出,因为= 是一个不求值的语句。您也可以通过执行fh.write(...); None 之类的操作来阻止输出。我不建议在实际代码中这样做,但为了限制输出,它可能是有益的。

    【讨论】:

      猜你喜欢
      • 2016-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2018-11-25
      • 1970-01-01
      • 2020-08-06
      • 1970-01-01
      相关资源
      最近更新 更多