如何将 Jupyter Notebook (.ipynb) 文件转换为 Python (.py) 文件
概要:编写python代码运行将.ipynb转化为.py
import json
def convert_ipynb_to_py(ipynb_file, py_file):
with open(ipynb_file, 'r',encoding='utf-8') as f:
notebook = json.load(f)
with open(py_file, 'w',encoding='utf-8') as f:
for cell in notebook['cells']:
if cell['cell_type'] == 'code':
f.write(''.join(cell['source']) + '\n\n')
convert_ipynb_to_py('counterfactual_adversarial_example_generation.ipynb', 'counterfactual_adversarial_example_generation.py')
然后执行这个代码就行
别的就是下载一个jupyter Note ,进行转化
或者在命令行里下载个jupyter 然后输入命令进行转换
pip install jupyter
然后,运行以下命令将你的 .ipynb 文件转换为 .py 文件——记得进入到你所在的.ipynb 文件目录下:
jupyter nbconvert --to script your_notebook.ipynb
所生成的文件在你所在的文件下
参考博客:https://blog.csdn.net/qq_69218005/article/details/131339237