【问题标题】:Python: Pretty Print in Jupyter NotebookPython:Jupyter Notebook 中的漂亮打印
【发布时间】:2023-03-31 04:22:01
【问题描述】:

我正在尝试在 Jupyter Notebook 中漂亮地打印一个字典。

我正在使用以下内容:

import pprint
stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)

但是按 shift+enter 后,没有出现 [out] 单元格(即我看不到漂亮的打印输出)

知道为什么会这样/我应该改变什么才能看到漂亮的打印输出吗?


编辑:实际上这是一个 python 2.7 问题 - 它在 3.x 中运行良好。 有没有人在 python 2.7 上试过并看到它工作?

【问题讨论】:

  • 相同的代码对我来说很好用。我正在使用 Python 3.6
  • @Ujjwal 哦,我明白了 - 我正在使用 2.7...你知道 2.7 中的 coreect 语法是什么吗?
  • 我在try.jupyter.org 上测试了你的代码,它确实显示了输出。
  • 你可以尝试输出 html,随心所欲:stackoverflow.com/questions/25698448/…
  • @jimbasquiat 是的,它适用于 2.7

标签: python python-2.7 jupyter-notebook python-2.x pretty-print


【解决方案1】:

这个标题比 Jupyter 中的 dict 更通用,所以我给出一个通用的答案。

打印的时候改一下

print(longValue, nextValue)

对此(必须尝试使用​​ 30 以外的其他值)

space = (30-len(longValue)*" "
print(longValue,space,nextValue)

这将改变这一点

进入这个

【讨论】:

    【解决方案2】:

    我在使用 Python 3 时遇到了这个问题,在我的情况下解决了删除 AD 块扩展的问题。它以某种方式阻止了设置输出的 AJAX 请求。

    【讨论】:

      【解决方案3】:

      无论您开发什么都是正确的。唯一可能的原因可能是 Jupyter Notebook 无法连接到服务器。如果您在工具栏中看到如下图所示连接到内核/服务器,请尝试刷新连接或重新加载页面。

      我使用了相同的代码,并且能够看到输出。检查下面的图片。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-03-06
        • 2014-01-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-22
        • 1970-01-01
        相关资源
        最近更新 更多