在 iPython 笔记本的代码单元格中 execution_count 使用什么值?

What value to use for execution_count in code cells of iPython notebooks?

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

我知道它应该设置为 null 但是 json 转储会自动在它周围加上引号,所以我在创建后执行搜索替换并删除它们。然而 ipython notebook 仍然对此不满意。我在 Python 2.7,iPython 笔记本 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, 之后也是如此。

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