如何将 Jupyter 笔记本转换为带有单元格分隔符(#%%)的 Python 脚本?

huangapple go评论160阅读模式
英文:

How to convert a jupyter notebook to a python script with cell delimiters (#%%)?

问题

如何将Jupyter笔记本转换为带有单元格分隔符(#%%)的Python脚本?
我已经检查了nbconvert,但似乎没有这个功能。此外,发现了相同的问题,答案似乎无法满足需求,因为Jupyter笔记本的实际原始源代码并没有按照这种结构组织。(最好能够一次性转换,而不是先使用nbconvert进行转换,然后进行模式匹配)

有没有推荐的工具?或者你能分享一个实现这个功能的脚本吗?

英文:

How to convert a jupyter notebook to a python script with cell delimiters (#%%)?
I've already checked nbconvert , but it doesn't seem to have the one. Also, the same question found, the answer doesn't satisfy the need because actual raw source codes of jupyter notebook isn't structured as such. (It'd be better to be able to convert at once, instead of converting with nbconvert first and then pattern matching)

Any tools recommended? Or could you share a script to achieve this?

答案1

得分: 1

这看起来与Jupytext处理的百分号分隔符相似,也可以查看这里的前几个命令。我所引用的具体命令是:

jupytext --to py:percent notebook.ipynb         # 将notebook.ipynb转换为双百分号格式的.py文件
jupytext --to py:percent --opt comment_magics=false notebook.ipynb   # 与上面相同 + 不注释魔术命令

有关最后一个命令和进一步的自定义选项,请参阅百分号格式部分的底部

英文:

That looks similar to the percent delimiter that Jupytext handles, see the top few commands here also. The specific commands I'm referencing:

jupytext --to py:percent notebook.ipynb         # convert notebook.ipynb to a .py file in the double percent format
jupytext --to py:percent --opt comment_magics=false notebook.ipynb   # same as above + do not comment magic commands

See the bottom of the percent format section for more about that last command and further customization options.

huangapple
  • 本文由 发表于 2023年6月1日 20:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定