【问题标题】:What does KeyError: 'text/plain' mean?KeyError: 'text/plain' 是什么意思?
【发布时间】:2015-12-15 03:55:15
【问题描述】:

阅读 Guido 的博客“Tail Recursion Elimination”后,我在玩耍时遇到了这个错误。

我很好奇嵌套字典是否存在非递归限制,并且我确实收到了一条错误消息,但只是在非常特定的情况下使用 IPython 控制台。

如果我降低 xrange(100 个作品),则没有错误。这里发生了什么?

thing = {}
thing2 = thing
for x in xrange(500):
    thing2[x] = {}
    thing2 = thing2[x]

thing

输出:

Traceback (most recent call last):

  File "<ipython-input-83-0b6d347b01d4>", line 1, in <module>
    thing

  File "C:\Python27\lib\site-packages\IPython\core\displayhook.py", line 255, in __call__
    self.log_output(format_dict)

  File "C:\Python27\lib\site-packages\IPython\core\displayhook.py", line 227, in log_output
    format_dict['text/plain']

KeyError: 'text/plain'


repr(thing)
Out[84]: '{0: {1: {2: {3: {4: ....{497: {498: {499: {}}}}}}}}'

【问题讨论】:

  • 无法重现同样的错误。
  • 这(可能)是 IPython 如何将数据输出到 HTML 页面的问题。对于它的价值,它在 Python 3.x 和 IPython-4.04 上也失败了,错误为maximum recursion depth exceeded while calling a Python object

标签: python python-2.7 ipython spyder


【解决方案1】:

这绝对是一个 IPython 问题。如果您尝试显示“事物”,它会失败(在 Python3 / IPython 4 中带有另一条错误消息)。但是,它是完全有效的对象。

这个(500 万级)没问题(虽然创建需要几秒钟):

thing = {}
thing2 = thing
for x in range(5000000):
    thing2[x] = {}
    thing2 = thing2[x]
thing;

【讨论】:

    猜你喜欢
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2012-07-28
    相关资源
    最近更新 更多