iPython笔记本加载不了:输出过多。

9

我有一个iPython笔记本文件无法加载,可能是因为文件中有太多输出(打印了数千行结果,旧计算机)。

我可以在notepad中轻松编辑文件,但从那里逐个单元格复制并清理代码非常耗时。

有没有一种不同的方法来恢复代码,或者要求iPython笔记本只在打开文件时加载代码而不打印所有过去的输出?


6
这对你有帮助吗?https://gist.github.com/minrk/6176788#file-nbstripout。当然,所有的功劳归于minrk :) - cel
太好了。非常感谢! - Alexis Eggermont
1个回答

4

我在Github上发现了一个输出删除脚本,感谢作者。

      import sys
      import io
      from IPython.nbformat import current


      def remove_outputs(nb):
          for ws in nb.worksheets:
              for cell in ws.cells:
                  if cell.cell_type == 'code':
                      cell.outputs = []`


      if __name__ == '__main__':
          fname = sys.argv[1]
          with io.open(fname, 'r') as f:
              nb = current.read(f, 'json')
              remove_outputs(nb)
              print current.writes(nb, 'json')

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接