Jupyter Notebook(.ipynb)文件转换为Python(.py)文件的3种方式

第一种

Jupyter Notebook(.ipynb)文件转换为Python(.py)文件的3种方式_第1张图片

第二种

jupyter nbconvert --to script xxx.ipynb

 第三种

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('D2NN-single-layer-CIFAR10(FO).ipynb', 'D2NN-single-layer-CIFAR10(FO)-gray.py')

你可能感兴趣的:(Python常用,python,jupyter,前端)