python 脚本错误信息不明确

python script ambiguous error message

我正在使用带有 C++ 内核的 jupiter notebooks 和 root 来创建一些交互式文档。该文档包括一些直方图和其他图表,因此我使用魔术 %%jsroot 以使图表具有交互性。神奇的是 javascript 而不仅仅是图像,因此它们可以进行交互。

一切正常,直到我尝试使用 nbconvert 将笔记本转换为 pdf 文件。由于无法将 javascript 转换为 pdf,我正在使用脚本删除我调用 %%jsroot 的单元格并重新 运行 笔记本以创建图像。 %%jsroot 命令在单独的单元格中调用。 这是我的脚本:

导入字符串 导入系统 infilename = sys.argv[1] 输出文件名 = sys.argv[2]

f = open(infilename)
lines = f.readlines()
f.close()
text = "".join(lines)

true=True
false=False

nbj = eval(text)

cells = nbj["cells"]

newcells=[]
jscount = 0
mustdecrement = False
for cell in cells:
   if cell["source"][0] == "%%jsroot on":
     mustdecrement = True
     jscount = 1
     continue
   else:
     if mustdecrement:
       cell["execution_count"] -= 1
     newcells.append(cell)
nbj["cells"] = newcells

if jscount == 1:
   newnb = open(outfilename,"w")
   newnb.write(str(nbj))
   newnb.close()

它给我错误:

Traceback (most recent call last): File "removeJS.py", line 28, in cell["execution_count"] -= 1 KeyError: 'execution_count'

知道这可能是什么吗?

我找到了这个(简单的逻辑错误)。 execution_count 属性 在降价单元格中不存在,因此未找到。