【问题标题】:What value to use for execution_count in code cells of iPython notebooks?iPython 笔记本的代码单元中的 execution_count 使用什么值?
【发布时间】:2016-01-09 21:04:39
【问题描述】:

在将 iPython 笔记本生成为 json 文件时,用作execution_count 输入参数的最佳值是什么?

我知道它应该设置为null,但是 json 转储会自动在它周围加上引号,所以我在创建后执行搜索替换并删除它们。但是 ipython notebook 仍然对此不满意。我在 Python 2.7,iPython notebook 4.0.5 上。

import json, fileinput, re

# Create the cell of python code
code_cell = {
    "cell_type": "code",
    "execution count": "null",
    "metadata": {
        "collapsed": False,
        "autoscroll": False,
    },
    "source": ["import os\n", "import numpy as np"],
    "outputs": [],
}

# Create ipython notebook dictionary
nbdict = { 'metadata': {}, \
    'nbformat': 4,
    'nbformat_minor': 0,
    'cells': [code_cell]
    }

with open("test.ipynb", 'w') as outfile:
    json.dump(nbdict, outfile)

# Strip double quotes from execution_count argument.  

file = fileinput.FileInput("test.ipynb", inplace=True)
for line in file:
    print(re.sub(r'"null"', 'null', line))
file.close()

我收到的错误是Notebook Validation failed: Additional properties are not allowed (u'execution count' was unexpected):

即使在我的 json 文件中有 "execution count": null, 之后也是如此。

【问题讨论】:

    标签: python json python-2.7 ipython-notebook


    【解决方案1】:

    看起来您缺少下划线:"execution count" 应该是 "execution_count",然后 null 应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2013-11-22
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2016-06-16
      相关资源
      最近更新 更多